Skip to content

Commit f23f39c

Browse files
committed
Sets encoding to null to preserve raw buffers
1 parent 647532f commit f23f39c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

spec/HTTPRequest.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,16 @@ describe("httpRequest", () => {
213213
done();
214214
}
215215
});
216+
});
217+
218+
it('should get a cat image', (done) => {
219+
httpRequest({
220+
url: 'http://thecatapi.com/api/images/get?format=src&type=jpg',
221+
followRedirects: true
222+
}).then((res) => {
223+
expect(res.buffer).not.toBe(null);
224+
expect(res.text).not.toBe(null);
225+
done();
226+
})
216227
})
217228
});

src/cloud-code/httpRequest.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ module.exports = function(options) {
4545
options = encodeBody(options);
4646
// set follow redirects to false by default
4747
options.followRedirect = options.followRedirects == true;
48-
48+
// force the response as a buffer
49+
options.encoding = null;
4950
request(options, (error, response, body) => {
5051
if (error) {
5152
if (callbacks.error) {
@@ -56,11 +57,11 @@ module.exports = function(options) {
5657
var httpResponse = {};
5758
httpResponse.status = response.statusCode;
5859
httpResponse.headers = response.headers;
59-
httpResponse.buffer = new Buffer(response.body);
60+
httpResponse.buffer = response.body;
6061
httpResponse.cookies = response.headers["set-cookie"];
61-
httpResponse.text = response.body;
62+
httpResponse.text = response.body.toString('utf-8');
6263
try {
63-
httpResponse.data = JSON.parse(response.body);
64+
httpResponse.data = JSON.parse(httpResponse.text);
6465
} catch (e) {}
6566
// Consider <200 && >= 400 as errors
6667
if (httpResponse.status < 200 || httpResponse.status >= 400) {

0 commit comments

Comments
 (0)