slimphp / Slim-Documentation

Old Slim Framework 3 documentation
16 stars 13 forks source link

Adding Content-Type description #7

Closed geggleto closed 9 years ago

geggleto commented 9 years ago

New users often seem confused regarding how slim populates params()/get()/post(). Content-Type application/json isn't supported by default, and users have been posting to a route with json data and not receiving anything from request->post().

We should improve the documentation with examples on how to correctly POST application/json data to a slim route, and now to receive that data.

geggleto commented 9 years ago

How to Deal with Content-Types

By default Slim does not parse any other content-type other than the standard form data because PHP does not support it. That means if you attempt to post application/json you will not be able to access the data via $app->request->post();

To solve this

You must parse the content type yourself. You can either do this on a per-route basis

//For application/json
$data = json_decode($app->request->getBody());

or use some pre-built middleware (https://github.com/slimphp/Slim-Middleware)