Issue:
Post variables aren't present while accessing through $app->request->post(), but accessible through $app->request->getBody().
Environment:
While making a postman POST call these post variables are accessible through both request->post() and request->getBody(). But, making a post call through angular $http same post variables are available only by request->getBody(). Not by request->post().
Please suggest!
What Slim version you use?
Sounds like an ContentType issue to me.
I've seen a difference in backend's behavior depending on the request Content-Type header.
Care to inspect what header are you sending? IIRC sending the Content Type "application/json" should allow Slim to access the variables both from $app->request->post() as well as the raw body.
Content-Type → text/html from postman which slim request says as [CONTENT_TYPE] => multipart/form-data;
And from angular its [CONTENT_TYPE] => application/json;charset=utf-8
just on the off chance, you have the json extension installed and configured in your environment ?
Source Code:
ContentTypes.php
...
protected function parseJson($input)
{
if (function_exists('json_decode')) {
$result = json_decode($input, true);
if ($result) {
return $result;
}
}
}
...
I think OP is mixing multipart/form-data post parameters and posted json body. In short when you POST multipart/form-data it will populate $_POST which you can also access via $app->request->post(). This is an PHPism.
When you post application/json data it will not populate $_POST and it will not be available from $app->request->post(). Instead you read the request body and parse the json yourself.
$body = $app->request->getBody();
$data = json_decode($body, true);
print_r($data);
That said there is middleware called ContentTypes which emulates the behaviour of multipart/form-data when using application/json data. The middleware decodes json automagically and populates $_POST and it will become available also in $app->request->post().
Thanks @tuupola for the explanation. That make sense :+1: . @geggleto I think i haven't enabled that extension. How to do that?
Run phpinfo() see if there's a Json header
@geggleto Yup, its enabled.I thought you were saying something else. I think i need to debug that middleware.
The middleware will not show phpinfo(). There you can see if your PHP has been compiled with JSON support. I think it has been default since 5.2.0. This has nothing to do with Slim middlewares.
You will save yourself some headaches by encoding the json manually in a route or a hook. It is just two lines of code.
@tuupola thanks once again, Yes i can do that, its simple :+1: . But, i really want to know why that middleware isn't working in my case?
IIRC json extension is part of PHP core since PHP 5.3. You don't need to install any additional json libs.
Edit: it was 5.2.0, my mistake.
Thanks everyone
@tuupola using ContentTypes middleware helps but it dosen't populates $_POST and it isn't in $app->request->post().
I just ran into this too as I'm trying to enable JSON input support. The ContentTypes middleware should populate environment['sim.request.form_hash'] instead of just 'slim.input'
Otherwise, you need two different functions to get fields depending on the content type. If it's JSON you use getBody() and if it's form data you use post(). It would make way more sense to just use post() for everything so that individual endpoints don't need to care about what the content type is.
This is with 2.6.2 btw.
True @rbalik , i used my own middleware for this.