axios: Error response data nodejs

Describe the bug

` var axios = require(‘axios’); var data = JSON.stringify({ “production_status”: “”, “package_status”: [], “extra_status_reason”: [], “page”: 1, “limit”: 1 });

var config = { method: ‘post’, url: ‘https://example.com/’, headers: { ‘Authorization’: ‘Bearer token’, }, data: { “production_status”: “”, “package_status”: [], “extra_status_reason”: [], “page”: 1, “limit”: 1 } };

axios(config) .then(function (response) { console.log(response.data); }) .catch(function (error) { console.log(error); }); `

When i send API this response image I want respose must be json like that image

To Reproduce

No response

Code snippet

No response

Expected behavior

No response

Axios Version

1.2.0

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

14.20.1

OS

No response

Additional Library Versions

No response

Additional context/Screenshots

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 71
  • Comments: 58 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Indeed, that workaround works in my case… based upon https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding I select identity:

    return axios.get(url, { headers: { Accept: 'application/json', 'Accept-Encoding': 'identity' }, params: { trophies: true } })

I spent my whole day because of this problem…😩

The problem is caused by https://github.com/axios/axios/pull/5250 cc @DigitalBrainJS @jasonsaayman

at this line:

const responseLength = +res.headers['content-length'];

If res.headers does not contain a content-length entry, then responeLength will be NaN

In that case this line:

if ((!responseLength || res.statusCode === 204) && res.headers['content-encoding']) {

Will cause a content-encoding header to be deleted and as a consequence axios will no longer unzip the response.

Can a maintainer confirm that manually adding Accept-Encoding: identity to every GET request is not the intended way to use axios (i.e., this issue is a regression)?

https://github.com/axios/axios/blob/a3d901777bc06e78adae3081f694cdbf76241303/test/unit/adapters/http.js#L469

@macductan Closing the issue without a fix is a bit misleading. And the workaround is far from ideal.

Maybe you’d consider reopening the issue until this is actually fixed?

The temporary workaround is to set Accept-Encoding header to something other than gzip, deflate, be - application/json for instance. And loose compressed data exchange of course.

I spent my entire day because of this same problem and fixed it by downgrading Axios to 1.1.3 🤣

.

this is ridiculous, other dependencies are reliant and system is down since yesterday

Try the latest release v1.2.1 please

Why has this been closed as completed? The issue still persists in axios@1.3.0. I don’t think that setting the Accept-Encoding: identify explicitly should be the favored solution.

The best solution is to go back to previous version 1.1.3 and wait for 1.2 to fix. There’s already a commit awaiting approval it seems to me.

I’ve started migrating all my applications to fetch https://blog.logrocket.com/fetch-api-node-js/

Ditto with GET https://lichess.org/api/user/Toadofsky?trophies=true

const axios = require('axios');
...
    const username = 'Toadofsky';
    const url = `https://lichess.org/api/user/${username}`;
    return axios.get(url, { headers: { Accept: 'application/json' }, params: { trophies: true } })
        .then(response => formatProfile(response.data))

Routing through proxy server BeeCeptor I can observe that Lichess response data are valid, however axios 1.2.0 does not handle the gzip encoding:

    _header: 'GET /api/user/Toadofsky?trophies=true HTTP/1.1\r\n' +
      'Accept: application/json\r\n' +
      'User-Agent: axios/1.2.0\r\n' +
      'Accept-Encoding: gzip, deflate, br\r\n' +
      'Host: lichess.free.beeceptor.com\r\n' +
      'Connection: close\r\n' +
      '\r\n',

I’m getting the same error after upgrading to 1.2.0 😦

More discussion here: #5296

No its close cause the PR that has the fix has been merged, this is how GitHub works. I will release this weekend

+1 @macductan, I saw you closed this issue. When do you think the fix be released? Thanks!

i downgraded axios to ^0.19.2 it works now

1.2.1 fixed issue, thanks. Drove me insane for hours…

You can always downgrade axios with no problems.

For those fixing this by downgrading to 1.1.3, if you are using typescript you may want to downgrade further to 1.1.0 to avoid https://github.com/axios/axios/issues/5011

For those that want to upgrade right now, use 1.2.0-alpha.1 (npm i axios@next)

I have the same problem after updating from 1.1.3 to 1.2.0

same error when using v1.2.0 data: '\x1F�\b\x00\x00\x00\x00\x00\x04\x03d�An�0\x10E��u�<cl�\\��\x02\x05���\x04\x04�U\ axios-bug

Downgrade to "axios": "1.1.3" for now.

Thanks @cetteup, I thought @macductan has already fixed that issue and closed it. If not, then it will be better to keep it opened since the workaround is not ideal because this issue regresses axios unzip feature for the returned result.

Same here. Locally it works, but not in a production env. I am getting the following response:

image

Setting “‘Accept-Encoding’: ‘identity’” in the headers resolves the problem (as a workaround).

Experienced this too, breaks the whole app. Downgraded to 1.1.3 for now. Interested in hearing if there is a recommended fix going forward.

We too are experiencing this issue.

Am I right in thinking that Accept-Encoding: identity is not a good fix as this would disable all encoding in HTTP responses, dramatically impacting performance?

We need a proper fix for this.

spent hours to find this page 🤣 change package.json version for axios to lower "axios": "~1.1.0" and wait for the fix to be released

Indeed, that workaround works in my case… based upon https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding I select identity:

    return axios.get(url, { headers: { Accept: 'application/json', 'Accept-Encoding': 'identity' }, params: { trophies: true } })

Thank you!! This fixed it!

I have the same problem since upgrading to 1.2.0 CleanShot 2022-11-23 at 13 10 47@2x