-
Notifications
You must be signed in to change notification settings - Fork 6
Add json parse when the request is 'application/x-www-form-urlencoded'. #100
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
base: master
Are you sure you want to change the base?
Conversation
The current json parse is only valid for requests of type 'application/json', so a valid parse for requests of type 'application/x-www-form-urlencoded' is added.
Fix JSHint error: "modules/ember-cli-fake-server/lib/json-utils.js: line 22, col 11, Misleading line break before '+'; readers may interpret this as an expression boundary."
@AMS777 Thanks for this PR! I'm somewhat wary of making the assumption that because the initial In addition, because the I'm not opposed to making the |
@bantic By no means pretends this change to be a complete solution to parsing the request data. It just adds one more request data type to be parsed. The JSON type is still the first case to be tried, and it fails if the request content does not match that type. Instead of loggin an exception message yet another parse try is performed, covering one request content type more. If it fails, the same exception message is logged so the same functionality as before is covered but with one less failing case. I did this parsing when testing I considered some methods to detect the request content type, like getting the headers, setting them as string, making a case-insensitve search for 'content-type' and checking if it's vaule contains 'json'. I saw it somewhat complicated and decided to follow the existing method: try to parse it no matter what it is and if it fails log an exception message. I think it's the simplest way, so I kept it. I also considered to make a separate, specific function |
@AMS777 Thanks for the thoughtful response. I'd be more interested in something like this: let json;
if (requestLooksLikeFormEncoded(request)) {
json = parseRequestAsFormEncoded(request.requestBody);
} else {
try {
json = JSON.parse(request.requestBody);
} catch(e) {
// ...
}
} If you agree and are ok restructuring it like that (and adding some tests), that would be great! |
Ok, @bantic, I'll take a look into it. Your solution is more clear and verbose, no doubt of it. I proposed the other solution just because it is simpler for the same result. |
I've restructured the request content type identification as you've proposed and have added 2 tests: test('json() function parses x-www-form-urlencoded request payload') |
@AMS777 thanks for the rev here! It might take me a few days to have a chance to review and respond fully, but I'll do so as soon as I can. |
The current json parse is only valid for requests of type 'application/json', so a valid parse for requests of type 'application/x-www-form-urlencoded' is added.