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

Set response encoding #20

Closed
nickdima opened this issue Oct 19, 2014 · 10 comments
Closed

Set response encoding #20

nickdima opened this issue Oct 19, 2014 · 10 comments
Milestone

Comments

@nickdima
Copy link

I wanted to get a buffer out of some binary data from a GET request but I couldn't get it to work unless I put res.setEncoding('binary') inside http.js
Any suggestions?

@mzabriskie
Copy link
Member

Would you mind sharing a Plunker with an example of your code?

@nickdima
Copy link
Author

This is a stripped down version of my code, in coffeescript

axios = require 'axios'
Zip = require 'adm-zip'

downloadSubitles = () ->
  axios
    url: 'http://subscene.com/subtitle/download?mac=PEuUfx42AOa6Sju0WvWBFlbLF_YeiqKwsB62_ws6DQH1khKyJy1_sj0-df29vIFw0'
  .then (response) ->
    buffer = new Buffer(response.data, 'binary')
    zip = new Zip(buffer)
    srt = zip.readAsText zip.getEntries()[0]

@mzabriskie
Copy link
Member

There is a responseType property in the config, but it only works in the browser at the moment. I think we could just leverage the same property and implement it for node.js.

@nickdima
Copy link
Author

That would be good!

@mzabriskie mzabriskie added the bug label Oct 21, 2014
@nickdima
Copy link
Author

I've been looking a bit into this, so I think maybe we could support buffer and string for the responseType in Node.js. When it's set to buffer we would read the response stream into a buffer and when set to string just set the encoding to utf-8 and concatenate the chunks.
I could prepare a PR if you like the idea.

@mzabriskie
Copy link
Member

@nickdima sorry I've totally neglected your issue. Yes, please submit a PR.

@nnarhinen
Copy link
Contributor

Also loosely related, responseType: 'arraybuffer' does not work in the browser because responseText property is not readable in such responses. Has to be read from response instead.

image

@sorenbs
Copy link

sorenbs commented Jan 28, 2015

Looking forward to this. My use case is websites that don't properly specify encoding in headers, but do specify it in a meta tag. So I need to parse as utf-8 first, and then possibly re-parse as different content type.

@nickdima
Copy link
Author

Sorry guys, I haven't use axios lately so I wont be able to work on that PR at this point. Maybe someone else could give it a try.

@mzabriskie mzabriskie added this to the 0.7.0 milestone Mar 19, 2015
@mzabriskie
Copy link
Member

I believe that this has been solved. See current http implementation https://github.com/mzabriskie/axios/blob/master/lib/adapters/http.js#L93-L96

@axios axios locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants