Skip to content

Commit

Permalink
default encoding is now querystring instead of JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Mar 8, 2016
1 parent 6acb5ce commit bd7d951
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
6 changes: 3 additions & 3 deletions spec/HTTPRequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ describe("httpRequest", () => {
})
});

it("should encode a JSON body by default", (done) => {
it("should encode a query string body by default", (done) => {
let options = {
body: {"foo": "bar"},
}
let result = httpRequest.encodeBody(options);
expect(result.body).toEqual('{"foo":"bar"}');
expect(result.headers['Content-Type']).toEqual('application/json');
expect(result.body).toEqual('foo=bar');
expect(result.headers['Content-Type']).toEqual('application/x-www-form-urlencoded');
done();

})
Expand Down
14 changes: 5 additions & 9 deletions src/cloud-code/httpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ var encodeBody = function({body, headers = {}}) {

if (contentTypeKeys.length == 0) {
// no content type
try {
body = JSON.stringify(body);
headers['Content-Type'] = 'application/json';
} catch(e) {
// do nothing;
}
// As per https://parse.com/docs/cloudcode/guide#cloud-code-advanced-sending-a-post-request the default encoding is supposedly x-www-form-urlencoded

body = querystring.stringify(body);
headers['Content-Type'] = 'application/x-www-form-urlencoded';
} else {
/* istanbul ignore next */
if (contentTypeKeys.length > 1) {
Expand All @@ -29,9 +27,7 @@ var encodeBody = function({body, headers = {}}) {
if (headers[contentType].match(/application\/json/i)) {
body = JSON.stringify(body);
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
body = Object.keys(body).map(function(key){
return `${key}=${encodeURIComponent(body[key])}`
}).join("&");
body = querystring.stringify(body);
}
}
return {body, headers};
Expand Down

0 comments on commit bd7d951

Please sign in to comment.