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

How do you read binary data? #147

Closed
caub opened this issue Aug 16, 2016 · 4 comments
Closed

How do you read binary data? #147

caub opened this issue Aug 16, 2016 · 4 comments

Comments

@caub
Copy link

caub commented Aug 16, 2016

It seems a few methods like .blob() are missing to do so

the idea is just making something similar to:

request('some.png', (err, res, body)=>{
    console.log(Buffer.from(body).toString('base64'));
});
@jimmywarting
Copy link
Collaborator

jimmywarting commented Aug 16, 2016

This is more of a stackoverflow question and not an issue or a bug


you can use res.body which is a stream...
So you can pipe it. Example: res.body.pipe(dest)
or do:

res.body.on('data', chunk => {
   // Got a chunked buffer
}).on('end' () => {
   // end of stream 
})

You now also have the alternativ method to browsers arrayBuffer which is called just buffer (as of v1.6.0) and it will resolve to a node buffer instance res.body.buffer()

fetch('some.png')
.then(res => res.buffer())
.then(console.log)

@caub
Copy link
Author

caub commented Aug 16, 2016

Great answer, thanks, sorry

hmm, note with 1.6.0:

fetch('https://small.sdsu.edu/sites/all/themes/zen_larc/widgets/larcrating/smile_small.png')
.then(r=>r.body.buffer())
// .then(r=>r.body.arrayBuffer())
.catch(console.error)

gives me:

TypeError: r.body.arrayBuffer is not a function / TypeError: r.body.buffer is not a function

@caub caub closed this as completed Aug 16, 2016
@caub caub reopened this Aug 16, 2016
@bitinn
Copy link
Collaborator

bitinn commented Aug 18, 2016

Yeah as documented in known differences and #51, on Node.js there isn't much point in using arrayBuffer.

@wengeezhang
Copy link

This is more of a stackoverflow question and not an issue or a bug

you can use res.body which is a stream...
So you can pipe it. Example: res.body.pipe(dest)
or do:

res.body.on('data', chunk => {
   // Got a chunked buffer
}).on('end' () => {
   // end of stream 
})

You now also have the alternativ method to browsers arrayBuffer which is called just buffer (as of v1.6.0) and it will resolve to a node buffer instance res.body.buffer()

fetch('some.png')
.then(res => res.body.buffer())
.then(console.log)

just use res.buffer(), not res.body.buffer(), as follows:
fetch('some.png')
.then(res => res.buffer())
.then(console.log)

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

4 participants