vidit mathur
vidit mathur

Reputation: 72

How does requset.get().pipe() works?

If I use

request.get(imageUrl).pipe(resposne)

then, does it return response with all data received from request.get() including headers and all the other data ?

Upvotes: 0

Views: 56

Answers (2)

s.d
s.d

Reputation: 29436

Assuming you are using the request module.

The request is a readable stream (That's why you can use the pipe() function). The readable data in stream here is the body of HTTP response.

To get the headers and status code as well, you can listen to events:

request
  .get('http://example.com/img.png')
  .on('response', function(response) {
    console.log(response.statusCode) // 200
    console.log(response.headers['content-type']) // 'image/png'
  })
  .pipe(request.put('http://example.com/img.png'))

Upvotes: 0

LuisPinto
LuisPinto

Reputation: 1697

You will use pipe when you want to stream a response.

For example

request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))

Or

request.get('http://google.com/img.png').pipe(request.put('https://xxxe.com/img.png)

You don't need to use pipe in your case as response.get will also contain all information.

Upvotes: 1

Related Questions