Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct use of Unirest.pipe #14

Closed
cosmicflame opened this issue May 12, 2014 · 8 comments
Closed

Correct use of Unirest.pipe #14

cosmicflame opened this issue May 12, 2014 · 8 comments

Comments

@cosmicflame
Copy link
Contributor

Hi, it's me again!

Quick question for you - I noticed that Unirest.pipe is defined as an alias to Unirest.request.pipe:
https://github.com/Mashape/unirest-nodejs/blob/master/index.js#L703
Unirest.pipe = Unirest.request.pipe;
This does not appear to documented in the readme.

I'm trying to download images using Unirest and have stumbled across a method that seems to work:

var unirest = require('unirest')
var fs = require('fs')

unirest.get("https://www.google.co.uk/logos/doodles/2014/dorothy-hodgkins-104th-birthday-born-1910-5134139112030208.3-hp.jpg")
  .end()
  .pipe(fs.createWriteStream(__dirname + '/myImg.jpg'))

Is this how Unirest.pipe is intended to be used, chained after .end(), or am I doing it wrong?

Thanks.

@nijikokun
Copy link
Contributor

This is a little tricky, pipe won't work on .end too much sugar going on there. This has to do with something I've been wanting to change around, but it will make things a little different going forward.

.end() does the whole shebang and instead I would rather return a request instance the whole way along instead. True aliasing.

@cosmicflame
Copy link
Contributor Author

It does actually work and save the file though... Presumably this is a matter of "might work sometimes, but not supported" as it were, then?

@nijikokun
Copy link
Contributor

Ah, it will work because it is returned:

https://github.com/Mashape/unirest-nodejs/blob/master/index.js#L489

However, it's not as pure as request.get().pipe(); would be.

@cosmicflame
Copy link
Contributor Author

So... a-ok correct usage of Pipe for writing files? :)

@nijikokun
Copy link
Contributor

File manipulation is easier using request directly right now in my opinion.

@cosmicflame
Copy link
Contributor Author

The Unirest API is much nicer, though ;)
I'm trying to get an app I'm working on down to using a single HTTP client library. If I'm reading your responses correctly (which is to say that it will work, but this is an area where Unirest will improve in the future) then mission accomplished! :)

@nijikokun
Copy link
Contributor

Alright, closing this then :)

@cosmicflame
Copy link
Contributor Author

Thanks for the help, and thanks most of all for unirest-nodejs - this library is making my job a whole lot easier and nicer! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants