Skip to content

Commit 0f7335b

Browse files
committed
Merge pull request #892 from ParsePlatform/flovilmart.httpRequestDefaultContentType
Default body to querystring, null encoding
2 parents 7346ff2 + bd7d951 commit 0f7335b

File tree

3 files changed

+90
-35
lines changed

3 files changed

+90
-35
lines changed

spec/HTTPRequest.spec.js

Lines changed: 44 additions & 15 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");
@@ -158,31 +160,47 @@ describe("httpRequest", () => {
158160
done();
159161
})
160162
});
161-
it("should encode a JSON body", (done) => {
163+
164+
it("should encode a query string body by default", (done) => {
165+
let options = {
166+
body: {"foo": "bar"},
167+
}
168+
let result = httpRequest.encodeBody(options);
169+
expect(result.body).toEqual('foo=bar');
170+
expect(result.headers['Content-Type']).toEqual('application/x-www-form-urlencoded');
171+
done();
162172

163-
var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'});
164-
expect(result).toEqual('{"foo":"bar"}');
173+
})
174+
175+
it("should encode a JSON body", (done) => {
176+
let options = {
177+
body: {"foo": "bar"},
178+
headers: {'Content-Type': 'application/json'}
179+
}
180+
let result = httpRequest.encodeBody(options);
181+
expect(result.body).toEqual('{"foo":"bar"}');
165182
done();
166183

167184
})
168185
it("should encode a www-form body", (done) => {
169-
170-
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'application/x-www-form-urlencoded'});
171-
expect(result).toEqual("foo=bar&bar=baz");
186+
let options = {
187+
body: {"foo": "bar", "bar": "baz"},
188+
headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'}
189+
}
190+
let result = httpRequest.encodeBody(options);
191+
expect(result.body).toEqual("foo=bar&bar=baz");
172192
done();
173193
});
174194
it("should not encode a wrong content type", (done) => {
175-
176-
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'mime/jpeg'});
177-
expect(result).toEqual({"foo": "bar", "bar": "baz"});
195+
let options = {
196+
body:{"foo": "bar", "bar": "baz"},
197+
headers: {'cOntent-tYpe': 'mime/jpeg'}
198+
}
199+
let result = httpRequest.encodeBody(options);
200+
expect(result.body).toEqual({"foo": "bar", "bar": "baz"});
178201
done();
179202
});
180-
it("should not encode when missing content type", (done) => {
181-
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'});
182-
expect(result).toEqual({"foo": "bar", "bar": "baz"});
183-
done();
184-
});
185-
203+
186204
it("should fail gracefully", (done) => {
187205
httpRequest({
188206
url: "http://not a good url",
@@ -196,6 +214,17 @@ describe("httpRequest", () => {
196214
done();
197215
}
198216
});
217+
});
218+
219+
it('should get a cat image', (done) => {
220+
httpRequest({
221+
url: 'http://thecatapi.com/api/images/get?format=src&type=jpg',
222+
followRedirects: true
223+
}).then((res) => {
224+
expect(res.buffer).not.toBe(null);
225+
expect(res.text).not.toBe(null);
226+
done();
227+
})
199228
})
200229

201230
it("should params object to query string", (done) => {

src/cloud-code/HTTPResponse.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
export default class HTTPResponse {
3+
constructor(response) {
4+
this.status = response.statusCode;
5+
this.headers = response.headers;
6+
this.buffer = response.body;
7+
this.cookies = response.headers["set-cookie"];
8+
}
9+
10+
get text() {
11+
return this.buffer.toString('utf-8');
12+
}
13+
get data() {
14+
if (!this._data) {
15+
try {
16+
this._data = JSON.parse(this.text);
17+
} catch (e) {}
18+
}
19+
return this._data;
20+
}
21+
}

src/cloud-code/httpRequest.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
var request = require("request"),
2-
querystring = require('querystring'),
3-
Parse = require('parse/node').Parse;
1+
import request from 'request';
2+
import Parse from 'parse/node';
3+
import HTTPResponse from './HTTPResponse';
4+
import querystring from 'querystring';
45

5-
var encodeBody = function(body, headers = {}) {
6+
var encodeBody = function({body, headers = {}}) {
67
if (typeof body !== 'object') {
7-
return body;
8+
return {body, headers};
89
}
910
var contentTypeKeys = Object.keys(headers).filter((key) => {
1011
return key.match(/content-type/i) != null;
1112
});
1213

13-
if (contentTypeKeys.length == 1) {
14+
if (contentTypeKeys.length == 0) {
15+
// no content type
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';
20+
} else {
21+
/* istanbul ignore next */
22+
if (contentTypeKeys.length > 1) {
23+
console.error('multiple content-type headers are set.');
24+
}
25+
// There maybe many, we'll just take the 1st one
1426
var contentType = contentTypeKeys[0];
1527
if (headers[contentType].match(/application\/json/i)) {
1628
body = JSON.stringify(body);
1729
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
18-
body = Object.keys(body).map(function(key){
19-
return `${key}=${encodeURIComponent(body[key])}`
20-
}).join("&");
30+
body = querystring.stringify(body);
2131
}
2232
}
23-
return body;
33+
return {body, headers};
2434
}
2535

2636
module.exports = function(options) {
@@ -32,7 +42,7 @@ module.exports = function(options) {
3242
delete options.success;
3343
delete options.error;
3444
delete options.uri; // not supported
35-
options.body = encodeBody(options.body, options.headers);
45+
options = Object.assign(options, encodeBody(options));
3646
// set follow redirects to false by default
3747
options.followRedirect = options.followRedirects == true;
3848
// support params options
@@ -41,6 +51,8 @@ module.exports = function(options) {
4151
} else if (typeof options.params === 'string') {
4252
options.qs = querystring.parse(options.params);
4353
}
54+
// force the response as a buffer
55+
options.encoding = null;
4456

4557
request(options, (error, response, body) => {
4658
if (error) {
@@ -49,15 +61,8 @@ module.exports = function(options) {
4961
}
5062
return promise.reject(error);
5163
}
52-
var httpResponse = {};
53-
httpResponse.status = response.statusCode;
54-
httpResponse.headers = response.headers;
55-
httpResponse.buffer = new Buffer(response.body);
56-
httpResponse.cookies = response.headers["set-cookie"];
57-
httpResponse.text = response.body;
58-
try {
59-
httpResponse.data = JSON.parse(response.body);
60-
} catch (e) {}
64+
let httpResponse = new HTTPResponse(response);
65+
6166
// Consider <200 && >= 400 as errors
6267
if (httpResponse.status < 200 || httpResponse.status >= 400) {
6368
if (callbacks.error) {

0 commit comments

Comments
 (0)