Nyholm / psr7

A super lightweight PSR-7 implementation
MIT License
1.16k stars 76 forks source link

ServerRequest::getParsedBody always returns empty array #100

Closed juliardi closed 5 years ago

juliardi commented 5 years ago

I am trying to get the request's parsed body using ServerRequest::getParsedBody method, but it always returns an empty array. I've checked the code and do not find the code related to parsing request's body. Is this the intended behaviour?

Zegnat commented 5 years ago

That sounds wrong. I would expect it to return null or whatever you set it to using ServerRequest::withParsedBody(). Do you have any code that shows the problem?

Nyholm commented 5 years ago

How do you create the server request? Could you give us 10-20 lines of example code?

juliardi commented 5 years ago

I am using the example code from https://github.com/Nyholm/psr7-server/blob/master/README.md. I'll check my code again to make sure.

Zegnat commented 5 years ago

Nyholm/psr7-server always puts PHP’s $_POST as the parsed body of a request, defaulting to an empty array. It doesn’t do any parsing itself, and will not try to parse non-form encoded requests. (Arguably it should default to null.)

There are a number of middleware available that will parse other types of request content, e.g. Zend includes a JSON parser in their Expressive Helpers.

juliardi commented 5 years ago

After checking the request, I found out that the request doesn't include Content-Type: application/x-www-form-urlencoded or Content-Type: multipart/form-data in its header. When the request included one of those headers, ServerRequest::getParsedBody works fine.