slimphp / Slim-Psr7

PSR-7 implementation for use with Slim 4
MIT License
132 stars 45 forks source link

HTTP POST parameters not detected when using jQuery $.ajax #38

Closed holtkamp closed 5 years ago

holtkamp commented 5 years ago

Context Currently, HTTP POST request parameters are detected when the ContentType of the HTTP Request equals either application/x-www-form-urlencoded or multipart/form-data. After this "detection", they can be access using $request->getParsedBody().

When using jQuery $.ajax to dispatch an HTTP POST request, the default ContentType of the HTTP Request is application/x-www-form-urlencoded; charset=UTF-8, which is valid syntax for this header.

Problem When using jQuery $.ajax to dispatch an HTTP POST request, the current approach does not properly detect this as being a HTTP POST request and $request->getParsedBody() remains empty, while $request->getBody() contains content like parameter1=value1&parameter2=value2&parameterN=valueN.

Workaround Currently the following workarounds can be considered:

Possible solution Maybe the current exact match for application/x-www-form-urlencoded can be loosened a bit? At least this is how the FormUrlEncodedStrategy of the BodyParsingMiddleware does it

A more robust / future proof approach might be to adopt a similar "strategy" mechanism as the BodyParsingMiddleware, which also has a strategy for detecting JSON payloads, also see https://github.com/zendframework/zend-expressive-helpers/tree/master/src/BodyParams

holtkamp commented 5 years ago

Wow, that is quick! Works great, thanks a bunch! 🎉