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

JSON POST body URI encoded (RFC3986) #1395

Closed
chacal opened this issue Feb 2, 2015 · 12 comments · Fixed by #1396
Closed

JSON POST body URI encoded (RFC3986) #1395

chacal opened this issue Feb 2, 2015 · 12 comments · Fixed by #1396

Comments

@chacal
Copy link

chacal commented Feb 2, 2015

Due to pull request #1315 all POSTed JSON content is URI encoded in 2.52.1.

Is there some way to avoid this? The server I'm POSTing to does not expect JSON data to be encoded, but instead sent as is.

Anyhow, is it OK to encode JSON content at all as the format itself allows the usage of e.g. '!' and '*' without any encoding? {"key": "value !"} is a very different JSON than {"key": "value %21"}

@nylen
Copy link
Member

nylen commented Feb 2, 2015

Thanks for reporting, I agree that we should not pass JSON POST bodies through rfc3986. @simov want to take a look so we can release a fixed version quickly?

@simov
Copy link
Member

simov commented Feb 2, 2015

That was the reason why I proposed this change as an option (first commit). If Twitter allows json post bodies (not sure) then there it should be rfc3986 encoded. So what's the expected behavior exactly?

@nylen
Copy link
Member

nylen commented Feb 2, 2015

Encoding regular application/x-www-form-urlencoded POST bodies is transparent: if we encode too much, the server will decode it.

I didn't think about this until now, but encoding JSON in this way is not transparent. I'd be surprised if there were any servers that will also URL-decode while decoding JSON.

Propose deleting this line, let me know what you think.

@simov
Copy link
Member

simov commented Feb 2, 2015

OK, I'll take a look, and submit PR (the tests should be changed a bit)

@chacal
Copy link
Author

chacal commented Feb 2, 2015

Shouldn't the encoding be removed also on line 1439? There we are setting JSON request body as well, I believe.

@simov
Copy link
Member

simov commented Feb 2, 2015

I'm just looking at it :)

@nylen
Copy link
Member

nylen commented Feb 2, 2015

Version 2.53.0 is on npm with this fix.

@chacal
Copy link
Author

chacal commented Feb 3, 2015

Thanks for the very fast response on this! Version 2.53.0 works like a charm! :)

@nylen
Copy link
Member

nylen commented Feb 3, 2015

glad to hear it, that was a nasty bug 😓

@crobinson42
Copy link

crobinson42 commented Aug 4, 2016

Hey @chacal @nylen I'm running into this using request to post to github using the github api. It's encoding my JSON and I can't seem to understand a way around it. Any help?

I send this { "title": "Test Bug Title..Kaboom!"} and github receives this { "title": "Test Bug Title..Kaboom%21" }

request
                .post({
                    url: "https://api.github.com/repos/crobinson42/therms-public/issues",
                    form: JSON.stringify(githubIssueObj),
                    headers: {
                        'User-Agent': 'App Auto Bug Filing',
                        'Authorization': 'Basic yaDaYadA...YadaYada',
                        'Content-type': 'application/json; charset=utf-8'
                    }
                },...

Using v2.74.0

Thanks!

@simov
Copy link
Member

simov commented Aug 4, 2016

@crobinson42 you are not supposed to send URL encoded JSON. If you really do need to send JSON then use the json option instead. For URL encoded body pass a regular object to the form option (without JSON.stringify).

@crobinson42
Copy link

@simov Ahh, THANK you for pointing that out! I added the option flag json:true and moved my JSON to the body: myJSON instead of form: myJSON and that did the trick.

request.post({
  url: "my url...",
  json: true,
  body: myJSON
});

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.

4 participants