Skip to content

Commit bd7d951

Browse files
committed
default encoding is now querystring instead of JSON
1 parent 6acb5ce commit bd7d951

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

spec/HTTPRequest.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ describe("httpRequest", () => {
161161
})
162162
});
163163

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

173173
})

src/cloud-code/httpRequest.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ var encodeBody = function({body, headers = {}}) {
1313

1414
if (contentTypeKeys.length == 0) {
1515
// no content type
16-
try {
17-
body = JSON.stringify(body);
18-
headers['Content-Type'] = 'application/json';
19-
} catch(e) {
20-
// do nothing;
21-
}
16+
// As per https://parse.com/docs/cloudcode/guide#cloud-code-advanced-sending-a-post-request the default encoding is supposedly x-www-form-urlencoded
17+
18+
body = querystring.stringify(body);
19+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
2220
} else {
2321
/* istanbul ignore next */
2422
if (contentTypeKeys.length > 1) {
@@ -29,9 +27,7 @@ var encodeBody = function({body, headers = {}}) {
2927
if (headers[contentType].match(/application\/json/i)) {
3028
body = JSON.stringify(body);
3129
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
32-
body = Object.keys(body).map(function(key){
33-
return `${key}=${encodeURIComponent(body[key])}`
34-
}).join("&");
30+
body = querystring.stringify(body);
3531
}
3632
}
3733
return {body, headers};

0 commit comments

Comments
 (0)