Skip to content

Commit 647532f

Browse files
committed
Encodes the body by default to application/json if is object
1 parent 75ae958 commit 647532f

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

spec/HTTPRequest.spec.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
var httpRequest = require("../src/cloud-code/httpRequest"),
24
bodyParser = require('body-parser'),
35
express = require("express");
@@ -154,31 +156,50 @@ describe("httpRequest", () => {
154156
done();
155157
})
156158
});
157-
it("should encode a JSON body", (done) => {
159+
it("should encode a JSON body by default", (done) => {
158160

159-
var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'});
160-
expect(result).toEqual('{"foo":"bar"}');
161+
let options = {
162+
body: {"foo": "bar"},
163+
}
164+
165+
var result = httpRequest.encodeBody(options);
166+
expect(result.body).toEqual('{"foo":"bar"}');
167+
expect(result.headers['Content-Type']).toEqual('application/json');
161168
done();
162169

163170
})
164-
it("should encode a www-form body", (done) => {
171+
172+
it("should encode a JSON body", (done) => {
173+
174+
let options = {
175+
body: {"foo": "bar"},
176+
headers: {'Content-Type': 'application/json'}
177+
}
165178

166-
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'application/x-www-form-urlencoded'});
167-
expect(result).toEqual("foo=bar&bar=baz");
179+
var result = httpRequest.encodeBody(options);
180+
expect(result.body).toEqual('{"foo":"bar"}');
168181
done();
169-
});
170-
it("should not encode a wrong content type", (done) => {
171182

172-
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'mime/jpeg'});
173-
expect(result).toEqual({"foo": "bar", "bar": "baz"});
183+
})
184+
it("should encode a www-form body", (done) => {
185+
let options = {
186+
body: {"foo": "bar", "bar": "baz"},
187+
headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'}
188+
}
189+
var result = httpRequest.encodeBody(options);
190+
expect(result.body).toEqual("foo=bar&bar=baz");
174191
done();
175192
});
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"});
193+
it("should not encode a wrong content type", (done) => {
194+
let options = {
195+
body:{"foo": "bar", "bar": "baz"},
196+
headers: {'cOntent-tYpe': 'mime/jpeg'}
197+
}
198+
var result = httpRequest.encodeBody(options);
199+
expect(result.body).toEqual({"foo": "bar", "bar": "baz"});
179200
done();
180201
});
181-
202+
182203
it("should fail gracefully", (done) => {
183204
httpRequest({
184205
url: "http://not a good url",

src/cloud-code/httpRequest.js

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

4-
var encodeBody = function(body, headers = {}) {
4+
var encodeBody = function(options = {}) {
5+
let body = options.body;
6+
let headers = options.headers || {};
57
if (typeof body !== 'object') {
6-
return body;
8+
return options;
79
}
810
var contentTypeKeys = Object.keys(headers).filter((key) => {
911
return key.match(/content-type/i) != null;
1012
});
1113

12-
if (contentTypeKeys.length == 1) {
14+
if (contentTypeKeys.length == 0) {
15+
// no content type
16+
try {
17+
options.body = JSON.stringify(body);
18+
options.headers = options.headers || {};
19+
options.headers['Content-Type'] = 'application/json';
20+
} catch(e) {
21+
// do nothing;
22+
}
23+
} else if (contentTypeKeys.length == 1) {
1324
var contentType = contentTypeKeys[0];
1425
if (headers[contentType].match(/application\/json/i)) {
15-
body = JSON.stringify(body);
26+
options.body = JSON.stringify(body);
1627
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
17-
body = Object.keys(body).map(function(key){
28+
options.body = Object.keys(body).map(function(key){
1829
return `${key}=${encodeURIComponent(body[key])}`
1930
}).join("&");
2031
}
2132
}
22-
return body;
33+
return options;
2334
}
2435

2536
module.exports = function(options) {
@@ -31,7 +42,7 @@ module.exports = function(options) {
3142
delete options.success;
3243
delete options.error;
3344
delete options.uri; // not supported
34-
options.body = encodeBody(options.body, options.headers);
45+
options = encodeBody(options);
3546
// set follow redirects to false by default
3647
options.followRedirect = options.followRedirects == true;
3748

0 commit comments

Comments
 (0)