default encoding is now querystring instead of JSON

This commit is contained in:
Florent Vilmart
2016-03-08 08:08:48 -05:00
parent 6acb5cee80
commit bd7d951ad6
2 changed files with 8 additions and 12 deletions

View File

@@ -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 = { let options = {
body: {"foo": "bar"}, body: {"foo": "bar"},
} }
let result = httpRequest.encodeBody(options); let result = httpRequest.encodeBody(options);
expect(result.body).toEqual('{"foo":"bar"}'); expect(result.body).toEqual('foo=bar');
expect(result.headers['Content-Type']).toEqual('application/json'); expect(result.headers['Content-Type']).toEqual('application/x-www-form-urlencoded');
done(); done();
}) })

View File

@@ -13,12 +13,10 @@ var encodeBody = function({body, headers = {}}) {
if (contentTypeKeys.length == 0) { if (contentTypeKeys.length == 0) {
// no content type // no content type
try { // 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 = JSON.stringify(body);
headers['Content-Type'] = 'application/json'; body = querystring.stringify(body);
} catch(e) { headers['Content-Type'] = 'application/x-www-form-urlencoded';
// do nothing;
}
} else { } else {
/* istanbul ignore next */ /* istanbul ignore next */
if (contentTypeKeys.length > 1) { if (contentTypeKeys.length > 1) {
@@ -29,9 +27,7 @@ var encodeBody = function({body, headers = {}}) {
if (headers[contentType].match(/application\/json/i)) { if (headers[contentType].match(/application\/json/i)) {
body = JSON.stringify(body); body = JSON.stringify(body);
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) { } else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
body = Object.keys(body).map(function(key){ body = querystring.stringify(body);
return `${key}=${encodeURIComponent(body[key])}`
}).join("&");
} }
} }
return {body, headers}; return {body, headers};