Sets encoding to null to preserve raw buffers
This commit is contained in:
@@ -217,6 +217,17 @@ describe("httpRequest", () => {
|
|||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get a cat image', (done) => {
|
||||||
|
httpRequest({
|
||||||
|
url: 'http://thecatapi.com/api/images/get?format=src&type=jpg',
|
||||||
|
followRedirects: true
|
||||||
|
}).then((res) => {
|
||||||
|
expect(res.buffer).not.toBe(null);
|
||||||
|
expect(res.text).not.toBe(null);
|
||||||
|
done();
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should params object to query string", (done) => {
|
it("should params object to query string", (done) => {
|
||||||
|
|||||||
@@ -52,6 +52,8 @@ module.exports = function(options) {
|
|||||||
} else if (typeof options.params === 'string') {
|
} else if (typeof options.params === 'string') {
|
||||||
options.qs = querystring.parse(options.params);
|
options.qs = querystring.parse(options.params);
|
||||||
}
|
}
|
||||||
|
// force the response as a buffer
|
||||||
|
options.encoding = null;
|
||||||
|
|
||||||
request(options, (error, response, body) => {
|
request(options, (error, response, body) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -63,11 +65,11 @@ module.exports = function(options) {
|
|||||||
var httpResponse = {};
|
var httpResponse = {};
|
||||||
httpResponse.status = response.statusCode;
|
httpResponse.status = response.statusCode;
|
||||||
httpResponse.headers = response.headers;
|
httpResponse.headers = response.headers;
|
||||||
httpResponse.buffer = new Buffer(response.body);
|
httpResponse.buffer = response.body;
|
||||||
httpResponse.cookies = response.headers["set-cookie"];
|
httpResponse.cookies = response.headers["set-cookie"];
|
||||||
httpResponse.text = response.body;
|
httpResponse.text = response.body.toString('utf-8');
|
||||||
try {
|
try {
|
||||||
httpResponse.data = JSON.parse(response.body);
|
httpResponse.data = JSON.parse(httpResponse.text);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
// Consider <200 && >= 400 as errors
|
// Consider <200 && >= 400 as errors
|
||||||
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
||||||
|
|||||||
Reference in New Issue
Block a user