diff --git a/spec/HTTPRequest.spec.js b/spec/HTTPRequest.spec.js index 1936feee..4bacd0fe 100644 --- a/spec/HTTPRequest.spec.js +++ b/spec/HTTPRequest.spec.js @@ -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(); }) diff --git a/src/cloud-code/httpRequest.js b/src/cloud-code/httpRequest.js index 740ba9f8..d78e6735 100644 --- a/src/cloud-code/httpRequest.js +++ b/src/cloud-code/httpRequest.js @@ -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) { @@ -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};