Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire a GET request and get the node Stream

I'm trying to send as formData, a stream from an image I get using request

The problem is that the request is fire after the formData request. Is there any way I can pipe the image request to recognize? but with the freedom of adding parameters to the formData?

e.g:

var req = request({
  method: 'POST',
  url: 'http://www.foo.bar/api/v1/tag/recognize',
  formData: {
    image_file: request('http://visual-recognition-demo.mybluemix.net/images/horses.jpg'),
    param2: 'value2'
  },
  json: true,
});

How do I fire:

request('http://visual-recognition-demo.mybluemix.net/images/horses.jpg') so the response can be used in req

UPDATE: Seems like the Content-Length header is missing in the
http://visual-recognition-demo.mybluemix.net/images/horses.jpg response
and you only get Transfer-Encoding: chunked

More details here

like image 725
German Attanasio Avatar asked Mar 13 '15 19:03

German Attanasio


People also ask

How to perform an HTTP GET request in Node JS?

There are many ways to perform an HTTP GET request in Node.js, depending on the abstraction level you want to use. The simplest way to perform an HTTP request using Node.js is to use the Axios library: However, Axios requires the use of a 3rd party library.

How do I create a GET-request in NodeJS?

To get started, let’s first create a NodeJS server. Create a get-request.js file and open it with your favorite text editor. Now, create a server by adding the following code. const http = require (“http”) const requestListener = function ( req, res) { res. end ( "Hello, World!"

What are streams in Node JS?

Streams are not a concept unique to Node.js. They were introduced in the Unix operating system decades ago, and programs can interact with each other passing streams through the pipe operator ( | ).

What happens when a GET request fails in JavaScript?

If the GET request fails, the error message will be displayed on the console. Inside the callback function, the data will be received in chunks. Therefore, you need a way of storing every chunk received until all the data is received. The next step is to parse the data once the request completes. That’s it!


1 Answers

Take a look this part of the docs. What you're describing is essentially this block

request.get('http://google.com/img.png').pipe(request.put('some_url'));  

Note the docs

When doing so, content-type and content-length are preserved in the PUT headers.

Also note, request will always return a Stream if you don't specify a callback. If you do provide a callback it tries to convert the response to a String. Use the encoding:null to get it to return raw bytes.
Example -

request({
   url: 'some_url', //your image
   encoding: null  //returns resp.body as bytes
}, callback..)

To chain calls together (run one after the other) , you can nest the callbacks or use promises. For example to run a request after another request completes -

var request = require('request');

//1st
request('first_url', function (error, response, body) {
  if (!error && response.statusCode == 200) {

      //2nd
      request('other_url', function (error, response, body) {   
         //process resp
      });
  }
});  

Or even better, convert the request callback code to Promises. See a promise library like Bluebird on how to do this.

Here's an example with bluebird (uses then to work in an iterative fasion)

var Promise = require("bluebird");
Promise.promisifyAll(require("request"));

request.getAsync('some_url').then(function(resp) {
   request.getAsync('some_other_url').then(..processing code..);
});
like image 102
Justin Maat Avatar answered Oct 14 '22 13:10

Justin Maat