Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to buffer an HTTP response using the request module?

I would like to stream the contents of an HTTP response to a variable. My goal is to get an image via request(), and store it in in MongoDB - but the image is always corrupted.

This is my code:

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.
    });
})

What is the best way to use Buffer/streams in this case?

like image 794
jamjam Avatar asked Jan 03 '13 19:01

jamjam


People also ask

What can the request () method from the HTTP module be used for?

The request method is part of Node's built-in http module. This module handles much of the low-level functionality needed to create servers, receive requests, send responses, and keep connections open.

What is buffer module in node js?

The buffers module provides a way of handling streams of binary data. The Buffer object is a global object in Node. js, and it is not necessary to import it using the require keyword.

What is the use of Request module in node js?

The request module is used to make HTTP calls. It is the simplest way of making HTTP calls in node. js using this request module. It follows redirects by default.

Which module is most popular node module for making HTTP requests?

Axios Module: Another library that can be used is Axios. This is a popular node. js module used to perform HTTP requests and supports all the latest browsers.


3 Answers

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

You only get a stream back from request if you don't provide a callback; request() returns a 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.

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.

    }); 
});
like image 85
josh3736 Avatar answered Oct 18 '22 18:10

josh3736


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.

like image 26
DenoFiend Avatar answered Oct 18 '22 19:10

DenoFiend


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.)

like image 1
7zark7 Avatar answered Oct 18 '22 20:10

7zark7