如何使用请求模块缓冲http响应?

时间:2021-02-08 20:16:19

I would to convert a incoming http response which is a stream and store the data in a variable. I don't much about node streams and I am struggling to do this properly.

我想转换传入的http响应,它是一个流并将数据存储在一个变量中。我对节点流并不多,我正在努力做到这一点。

var request = require('request');

request('http://google.com/doodle.png', function (error, response, body) {

     // buffer the stream response to and a string variable.   
})

UPDATE

UPDATE

This my full code. My goal is to get the image with request and store it in mongodb. But the image is always corrupted. I thought because request response was a stream, the image was only been partially saved and hence the corruption.

这是我的完整代码。我的目标是通过请求获取图像并将其存储在mongodb中。但是图像总是被破坏了。我认为因为请求响应是一个流,图像只是部分保存,因此腐败。

request('http://google.com/doodle.png', function (error, response, body) {

    image = new Buffer(body, 'binary');

    db.images.insert({ filename: 'google.png', imgData: image}, function (err) {

        // handle errors etc.

    });

})

Now that you have clarified that request buffer's the response any idea on how I can properly save the image without a corruption.

现在您已经澄清了请求缓冲区的响应,我知道如何正确保存图像而不会损坏。

3 个解决方案

#1


34  

The request module buffers the response for you. In the callback, body is a string (or Buffer).

请求模块为您缓冲响应。在回调中,body是一个字符串(或Buffer)。

You only get a stream back from request if you don't provide a callback; request() returns a Stream.

如果您不提供回调,则只能从请求中获取流; request()返回一个Stream。

See the docs for more detail and examples.

有关更多详细信息和示例,请参阅文档。


request assumes that the response is text, so it tries to convert the response body into a sring (regardless of the MIME type). This will corrupt binary data. If you want to get the raw bytes, specify a null encoding.

请求假定响应是文本,因此它尝试将响应主体转换为sring(无论MIME类型如何)。这将破坏二进制数据。如果要获取原始字节,请指定空编码。

request({url:'http://google.com/doodle.png', encoding:null}, function (error, response, body) {
    db.images.insert({ filename: 'google.png', imgData: body}, function (err) {

        // handle errors etc.

    }); 
});

#2


2  

var options = {
    headers: {
        'Content-Length': contentLength,
        'Content-Type': 'application/octet-stream'
    },
    url: 'http://localhost:3000/lottery/lt',
    body: formData,
    encoding: null, // make response body to Buffer.
    method: 'POST'
};

set encoding to null, return Buffer.

将encoding设置为null,返回Buffer。

#3


0  

Have you tried piping this?:

你尝试过这个吗?:

request.get('http://google.com/doodle.png').pipe(request.put('{your mongo path}'))

(Though not familiar enough with Mongo to know if it supports direct inserts of binary data like this, I know CouchDB and Riak do.)

(虽然对Mongo不太熟悉,知道它是否支持直接插入这样的二进制数据,但我知道CouchDB和Riak会这样做。)

#1


34  

The request module buffers the response for you. In the callback, body is a string (or Buffer).

请求模块为您缓冲响应。在回调中,body是一个字符串(或Buffer)。

You only get a stream back from request if you don't provide a callback; request() returns a Stream.

如果您不提供回调,则只能从请求中获取流; request()返回一个Stream。

See the docs for more detail and examples.

有关更多详细信息和示例,请参阅文档。


request assumes that the response is text, so it tries to convert the response body into a sring (regardless of the MIME type). This will corrupt binary data. If you want to get the raw bytes, specify a null encoding.

请求假定响应是文本,因此它尝试将响应主体转换为sring(无论MIME类型如何)。这将破坏二进制数据。如果要获取原始字节,请指定空编码。

request({url:'http://google.com/doodle.png', encoding:null}, function (error, response, body) {
    db.images.insert({ filename: 'google.png', imgData: body}, function (err) {

        // handle errors etc.

    }); 
});

#2


2  

var options = {
    headers: {
        'Content-Length': contentLength,
        'Content-Type': 'application/octet-stream'
    },
    url: 'http://localhost:3000/lottery/lt',
    body: formData,
    encoding: null, // make response body to Buffer.
    method: 'POST'
};

set encoding to null, return Buffer.

将encoding设置为null,返回Buffer。

#3


0  

Have you tried piping this?:

你尝试过这个吗?:

request.get('http://google.com/doodle.png').pipe(request.put('{your mongo path}'))

(Though not familiar enough with Mongo to know if it supports direct inserts of binary data like this, I know CouchDB and Riak do.)

(虽然对Mongo不太熟悉,知道它是否支持直接插入这样的二进制数据,但我知道CouchDB和Riak会这样做。)