Skip to content

Commit b6223f5

Browse files
committed
Adds support for application/x-www-form-urlencoded
- Now the body encoding is inferred on the headers as supposed
1 parent cc3d93f commit b6223f5

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

spec/HTTPRequest.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ describe("httpRequest", () => {
139139
body: {
140140
foo: "bar"
141141
},
142+
headers: {
143+
'Content-Type': 'application/json'
144+
},
142145
success: function() { calls++; },
143146
error: function() { calls++; }
144147
}).then(function(httpResponse){
@@ -150,5 +153,29 @@ describe("httpRequest", () => {
150153
fail("should not fail");
151154
done();
152155
})
156+
});
157+
it("should encode a JSON body", (done) => {
158+
159+
var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'});
160+
expect(result).toEqual('{"foo":"bar"}');
161+
done();
162+
163+
})
164+
it("should encode a www-form body", (done) => {
165+
166+
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'application/x-www-form-urlencoded'});
167+
expect(result).toEqual("foo=bar&bar=baz");
168+
done();
169+
});
170+
it("should not encode a wrong content type", (done) => {
171+
172+
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'mime/jpeg'});
173+
expect(result).toEqual({"foo": "bar", "bar": "baz"});
174+
done();
175+
});
176+
it("should not encode when missing content type", (done) => {
177+
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'});
178+
expect(result).toEqual({"foo": "bar", "bar": "baz"});
179+
done();
153180
})
154181
});

src/httpRequest.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
var request = require("request"),
22
Parse = require('parse/node').Parse;
33

4+
var encodeBody = function(body, headers = {}) {
5+
if (typeof body !== 'object') {
6+
return body;
7+
}
8+
var contentTypeKeys = Object.keys(headers).filter((key) => {
9+
return key.match(/content-type/i) != null;
10+
});
11+
12+
if (contentTypeKeys.length == 1) {
13+
var contentType = contentTypeKeys[0];
14+
if (headers[contentType].match(/application\/json/i)) {
15+
body = JSON.stringify(body);
16+
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
17+
body = Object.keys(body).map(function(key){
18+
return `${key}=${encodeURIComponent(body[key])}`
19+
}).join("&");
20+
}
21+
}
22+
return body;
23+
}
24+
425
module.exports = function(options) {
526
var promise = new Parse.Promise();
627
var callbacks = {
@@ -10,13 +31,9 @@ module.exports = function(options) {
1031
delete options.success;
1132
delete options.error;
1233
delete options.uri; // not supported
13-
if (typeof options.body === 'object') {
14-
options.body = JSON.stringify(options.body);
15-
options.headers = options.headers || {};
16-
options.headers['Content-Type'] = "application/json";
17-
}
34+
options.body = encodeBody(options.body, options.headers);
1835
// set follow redirects to false by default
19-
options.followRedirect = options.followRedirects == true ? true : false;
36+
options.followRedirect = options.followRedirects == true;
2037

2138
request(options, (error, response, body) => {
2239
var httpResponse = {};
@@ -42,4 +59,6 @@ module.exports = function(options) {
4259
}
4360
});
4461
return promise;
45-
};
62+
};
63+
64+
module.exports.encodeBody = encodeBody;

0 commit comments

Comments
 (0)