简体   繁体   中英

Slim framework - how does post method work?

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') ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM