Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
holtkamp opened this issue Apr 27, 2019 · 1 comment · Fixed by #46
Closed

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

holtkamp opened this issue Apr 27, 2019 · 1 comment · Fixed by #46

Comments

@holtkamp
Copy link

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:

  • use ZendExpressive Body Parsing Middleware which does properly detect the HTTP POST request when the ContentType is application/x-www-form-urlencoded; charset=UTF-8, but this requires the complete set of Zend-Expressive Middleware helpers to be installed.
  • explicitly set the $.ajax ContentType to application/x-www-form-urlencoded

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant