Skip to content

Commit f40efe3

Browse files
committed
Sets encoding to null to preserve raw buffers
1 parent 8300dd8 commit f40efe3

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

spec/HTTPRequest.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ describe("httpRequest", () => {
217217
done();
218218
}
219219
});
220+
});
221+
222+
it('should get a cat image', (done) => {
223+
httpRequest({
224+
url: 'http://thecatapi.com/api/images/get?format=src&type=jpg',
225+
followRedirects: true
226+
}).then((res) => {
227+
expect(res.buffer).not.toBe(null);
228+
expect(res.text).not.toBe(null);
229+
done();
230+
})
220231
})
221232

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

src/cloud-code/httpRequest.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ module.exports = function(options) {
5252
} else if (typeof options.params === 'string') {
5353
options.qs = querystring.parse(options.params);
5454
}
55+
// force the response as a buffer
56+
options.encoding = null;
5557

5658
request(options, (error, response, body) => {
5759
if (error) {
@@ -63,11 +65,11 @@ module.exports = function(options) {
6365
var httpResponse = {};
6466
httpResponse.status = response.statusCode;
6567
httpResponse.headers = response.headers;
66-
httpResponse.buffer = new Buffer(response.body);
68+
httpResponse.buffer = response.body;
6769
httpResponse.cookies = response.headers["set-cookie"];
68-
httpResponse.text = response.body;
70+
httpResponse.text = response.body.toString('utf-8');
6971
try {
70-
httpResponse.data = JSON.parse(response.body);
72+
httpResponse.data = JSON.parse(httpResponse.text);
7173
} catch (e) {}
7274
// Consider <200 && >= 400 as errors
7375
if (httpResponse.status < 200 || httpResponse.status >= 400) {

0 commit comments

Comments
 (0)