doulian4467 2014-09-24 16:34
浏览 42
已采纳

超薄框架 - post方法如何工作?

I have written a rest service application.

I send this json request to the service:

POST /AndroidEvent/index.php/api/events HTTP/1.1
Host: localhost
Content-Type: application/json
Cache-Control: no-cache

{ "data": { "name": "ApiTest", "city": "Budapest", "place" : "Budapest 1082 Népszínház utca 18.", "event_start_date" :  "2014-09-01 00:00:00", "event_end_date" : "2014-09-21 00:00:00", "decription" : "Ez egy leírás ide", "category_id" : 1, "owner_id": 2, "coord_latitude" : "47.4544", "coord_longitude" : "47.4544", "picture_path" : "http://localhost/picture_path", "is_adult" : 0, "created_on" : "2014-09-21 00:00:00", "updated_on" : "" } }

But on the server side in the routes.php I could not get the post data in this way:

$app->request->post('data') // I will get an empty array.

But in this way I can get the post data:

json_decode($app->request->getBody());

Is it impossible to get the post data with the $app->request->post('data') ?

  • 写回答

1条回答 默认 最新

  • dongtang5776 2014-09-24 16:45
    关注

    You're sending a json string in a http request body using the POST http method. So in order to read it correct way is to json decode the body and then proceed. Just like you're doing it json_decode($app->request->getBody());

    Web forms with method="post" encode inputs in a query string and then put that in a body. Variables sent that way can be accessed with $app->request->post(<input_name>);

    The confusion here I think is that in $app->request->post(<input_name>);post actually refers to the php's $_POST superglobal which holds the decoded query stirng for you automatically. It is not the same as http method (verb) POST.

    http://docs.slimframework.com/#Request-Variables

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器