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

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

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境