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

Adds option to specify character set in responses (with http adapter) #869

Merged
merged 1 commit into from
Mar 10, 2018

Conversation

johntron
Copy link
Contributor

We have a service that returns responses encoded with the ISO-8859-1 character set, and needed a way to decode these correctly. This change allows us to decode correctly in our server:

axios.post('http://service-with-latin-encoding.com', {}, { charset: 'latin1' })
  .then(/* do something */);

@coveralls
Copy link

Coverage Status

Coverage increased (+1.5%) to 93.719% when pulling 5c825a7 on johntron:master into f31317a on mzabriskie:master.

@johntron johntron force-pushed the master branch 3 times, most recently from 83406f8 to f600705 Compare June 23, 2017 20:57
@johntron
Copy link
Contributor Author

@nickuraltsev @rubennorte thanks for maintaining this project. Would you mind taking a look at this PR? It's a simple, backwards-compatible improvement for server-side requests - even comes with updated README :D

@coveralls
Copy link

Coverage Status

Coverage remained the same at 93.734% when pulling 83406f8 on johntron:master into 46e275c on mzabriskie:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 93.734% when pulling f600705 on johntron:master into 46e275c on mzabriskie:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 93.734% when pulling f600705 on johntron:master into 46e275c on mzabriskie:master.

@@ -185,7 +185,7 @@ module.exports = function httpAdapter(config) {
stream.on('end', function handleStreamEnd() {
var responseData = Buffer.concat(responseBuffer);
if (config.responseType !== 'arraybuffer') {
responseData = responseData.toString('utf8');
responseData = responseData.toString(config.charset || 'utf-8');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is utf8 (without the dash) but as it's the Node Buffer default value you don't need to set the || 'utf8, you can just pass the config.charset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

README.md Outdated
@@ -280,6 +280,10 @@ These are the available config options for making requests. Only the `url` is re
// options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
responseType: 'json', // default

// `charset` indicates charset to use for decoding responses
// Note: Ignored for `responseType` of 'stream' or client-side requests
charset: 'utf-8', // default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use responseEncoding (adding the response prefix to make it clearer and using encoding to match the Node.js naming).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

@johntron
Copy link
Contributor Author

Thanks so much @rubennorte - I'll make those changes when I get home.

@johntron
Copy link
Contributor Author

Updated

@coveralls
Copy link

Coverage Status

Coverage remained the same at 93.812% when pulling 7340c5d on johntron:master into 07a7b7c on mzabriskie:master.

@tnhu
Copy link

tnhu commented Oct 5, 2017

I think it would be better to extract charset from response header content-type. This way you don't have to manually pass the charset per request.

@johntron
Copy link
Contributor Author

johntron commented Oct 8, 2017 via email

@johntron
Copy link
Contributor Author

johntron commented Mar 2, 2018

@rubennorte I made those changes in August of last year. Can this be merged yet?

@mawrkus
Copy link
Contributor

mawrkus commented Mar 10, 2018

Hi @rubennorte ! Any progress on this? I have a similar use case.

@nickuraltsev
Copy link
Member

@johntron Sorry for the delay! I'm going to merge it now.

@nickuraltsev nickuraltsev merged commit 9a6abd7 into axios:master Mar 10, 2018
@mawrkus
Copy link
Contributor

mawrkus commented Mar 11, 2018

Thanks for the merge!

@lepture
Copy link

lepture commented May 6, 2018

The implementation would be better in this way:

  1. responseEncoding default value should be null
  2. when responseEncoding has a value, use this encoding
  3. otherwise use the charset in Content-Type header
  4. fallback to utf-8

It seems that you need to use iconv-lite, since the default supported encodings in Node is very limited.

@johntron
Copy link
Contributor Author

johntron commented May 6, 2018

@lepture this PR has already been merged, but feel free to submit another PR. This one served my needs, and I would not like the added dependency for other encodings - I've never worked with an API that returned anything other than UTF-8 or latin-1.

@axios axios locked and limited conversation to collaborators May 3, 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

Successfully merging this pull request may close these issues.

7 participants