axios icon indicating copy to clipboard operation
axios copied to clipboard

AxiosError: maxContentLength size of -1 exceeded

Open MarianoFacundoArch opened this issue 2 years ago • 32 comments

Doing an axiosRequest with const a = await axios.get(URL)

and without any further configuration, it gives error: ' AxiosError: maxContentLength size of -1 exceeded'

The contentLength of the website is significant, >185KB, but as I did not configure any maxContentLength then this behaviour is nonsense.

What's the fix? Tried configuring a huge maxContentLength but still shows the error

MarianoFacundoArch avatar Jun 23 '22 17:06 MarianoFacundoArch

I have a similar issue when using proxy-agent and the proxy returning 407 I can see the real error hiding in the response error "Proxy Authentication Required"

{
  code: 'ERR_BAD_RESPONSE',
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [Function: httpAdapter],
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    env: { FormData: [Function] },
    validateStatus: [Function: validateStatus],
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.27.2'
    },
    httpsAgent: ProxyAgent {
      proxy: [Url],
      proxyUri: 'http://user:[email protected]:3228',
      proxyFn: [Function: httpOrHttpsProxy],
      promisifiedCallback: [Function (anonymous)]
    },
    httpAgent: ProxyAgent {
      proxy: [Url],
      proxyUri: 'http://user:[email protected]:3228',
      proxyFn: [Function: httpOrHttpsProxy],
      promisifiedCallback: [Function (anonymous)]
    },
    method: 'get',
    url: 'https://www.google.com',
    data: undefined
  },
  request: <ref *1> ClientRequest {
    _events: [Object: null prototype] {
      abort: [Function (anonymous)],
      aborted: [Function (anonymous)],
      connect: [Function (anonymous)],
      error: [Function (anonymous)],
      socket: [Function (anonymous)],
      timeout: [Function (anonymous)],
      prefinish: [Function: requestOnPrefinish]
    },
    _eventsCount: 7,
    _maxListeners: undefined,
    outputData: [],
    outputSize: 128,
    writable: true,
    destroyed: true,
    _last: true,
    chunkedEncoding: false,
    shouldKeepAlive: false,
    _defaultKeepAlive: true,
    useChunkedEncodingByDefault: false,
    sendDate: false,
    _removedConnection: false,
    _removedContLen: false,
    _removedTE: false,
    _contentLength: 0,
    _hasBody: true,
    _trailer: '',
    finished: true,
    _headerSent: true,
    socket: Socket {
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: null,
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 8,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: null,
      _server: null,
      parser: null,
      _httpMessage: [Circular *1],
      write: [Function: writeAfterFIN],
      [Symbol(async_id_symbol)]: -1,
      [Symbol(kHandle)]: null,
      [Symbol(kSetNoDelay)]: false,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: null,
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kCapture)]: false,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(RequestTimeout)]: undefined
    },
    _header: 'GET / HTTP/1.1\r\n' +
      'Accept: application/json, text/plain, */*\r\n' +
      'User-Agent: axios/0.27.2\r\n' +
      'Host: www.google.com\r\n' +
      'Connection: close\r\n' +
      '\r\n',
    _keepAliveTimeout: 0,
    _onPendingData: [Function: noopPendingOutput],
    agent: ProxyAgent {
      proxy: [Url],
      proxyUri: 'http://user:[email protected]:3228',
      proxyFn: [Function: httpOrHttpsProxy],
      promisifiedCallback: [Function (anonymous)]
    },
    socketPath: undefined,
    method: 'GET',
    maxHeaderSize: undefined,
    insecureHTTPParser: undefined,
    path: '/',
    _ended: false,
    res: IncomingMessage {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 4,
      _maxListeners: undefined,
      socket: [Socket],
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: false,
      headers: [Object],
      rawHeaders: [Array],
      trailers: {},
      rawTrailers: [],
      aborted: true,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 407,
      statusMessage: 'Proxy Authentication Required',
      client: [Socket],
      _consuming: true,
      _dumped: false,
      req: [Circular *1],
      responseUrl: 'https://www.google.com/',
      redirects: [],
      [Symbol(kCapture)]: false,
      [Symbol(RequestTimeout)]: undefined
    },
    aborted: false,
    timeoutCb: null,
    upgradeOrConnect: false,
    parser: null,
    maxHeadersCount: null,
    reusedSocket: false,
    host: 'www.google.com',
    protocol: 'https:',
    _redirectable: Writable {
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      _options: [Object],
      _ended: true,
      _ending: true,
      _redirectCount: 0,
      _redirects: [],
      _requestBodyLength: 0,
      _requestBodyBuffers: [],
      _onNativeResponse: [Function (anonymous)],
      _currentRequest: [Circular *1],
      _currentUrl: 'https://www.google.com/',
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false,
    [Symbol(kNeedDrain)]: false,
    [Symbol(corked)]: 0,
    [Symbol(kOutHeaders)]: [Object: null prototype] {
      accept: [Array],
      'user-agent': [Array],
      host: [Array]
    }
  }
}

arossert avatar Jul 05 '22 16:07 arossert

I am getting the same error when I try to post files using multipart/form-data https://github.com/axios/axios/issues/4885

colesiegel avatar Jul 26 '22 17:07 colesiegel

any update?

chenggaw avatar Aug 09 '22 12:08 chenggaw

any update? is somewhere trigger response aborted? unlikely is server-side issue.

zhujun24 avatar Aug 10 '22 14:08 zhujun24

AFAICT the error is raised here, in response to an aborted event on the response stream. The message is nonsense when maxContentLength is set to -1 so I guess you should just ignore it and treat the error as a generic "request aborted" failure.

pkaminski avatar Aug 16 '22 20:08 pkaminski

It actually obscures the real errors (e.g. 407 as mentioned in post by @arossert)

Other errors are also incorrectly handled by axios and hidden, see https://github.com/axios/axios/issues/4888

It makes it hard to know what your API should do when we cannot properly differentiate cause of errors, and correct status code is not returned.

colesiegel avatar Aug 17 '22 16:08 colesiegel

I also have the same problem.

gujixingxing avatar Aug 19 '22 04:08 gujixingxing

same here

bsonmez avatar Sep 20 '22 19:09 bsonmez

Any updates on this?

calinbogdan avatar Sep 26 '22 08:09 calinbogdan

I am facing same problem too.

UtpalKumarJha avatar Sep 27 '22 10:09 UtpalKumarJha

and update?

tigerinus avatar Oct 04 '22 01:10 tigerinus

I get same issue.

jalethesh avatar Nov 15 '22 15:11 jalethesh

I get same issue.

jalethesh avatar Nov 15 '22 15:11 jalethesh

OK. Thanks.

On Tue, Nov 15, 2022 at 1:17 PM Sergey S. Volkov @.***> wrote:

@jalethesh https://github.com/jalethesh fixed in latest versions

— Reply to this email directly, view it on GitHub https://github.com/axios/axios/issues/4806#issuecomment-1315697392, or unsubscribe https://github.com/notifications/unsubscribe-auth/AX64HJ2LHNVD53SH4UUTS2DWIPHUJANCNFSM5ZVF7ZGA . You are receiving this because you were mentioned.Message ID: @.***>

jalethesh avatar Nov 15 '22 18:11 jalethesh

Is this fixed now? I am confused by the update above ^

colesiegel avatar Nov 15 '22 19:11 colesiegel

>

@jalethesh @colesiegel

Sorry, I thought this was about swagger-typescript-api and create autoreply for this message ahahah,
swagger-typescript-api had the same issue, then I just removed axios and replace it to node-fetch

js2me avatar Nov 16 '22 04:11 js2me

I'm having problems with this too. Will do some investigating myself to see if i can determine the problem.

pincombe avatar Dec 07 '22 11:12 pincombe

I recently fixed the issue. In our cases, it was usually related to WordPress with a cache plugin. If you try curl https://your-domain, the response is broken too usually.

The fix is adding this header in your request:

"Accept-Encoding": "gzip, deflate",

You can read this for more info: https://github.com/louislam/uptime-kuma/issues/2253

Commit: https://github.com/louislam/uptime-kuma/commit/3e68cf2a1cfab8cb8cc7969c915fba5535c7d827

louislam avatar Dec 08 '22 15:12 louislam

This issue occurs to me with v1.2.2. My server sends tens of thousands of similar type POST requests every day. But, I met this issue only one time. The POST request uses JSON type data which has just 4 properties.

anselmo-coolstay avatar Jan 30 '23 07:01 anselmo-coolstay

no updated?

1c7 avatar Feb 04 '23 16:02 1c7

I created PR. That's now in review. https://github.com/axios/axios/pull/4917

mori5321 avatar Mar 10 '23 08:03 mori5321

Me, when my 2MB image can't be fetched because the max content size is 4MB

image

Johnrobmiller avatar May 05 '23 02:05 Johnrobmiller

Any update on this? Can we at least merge #4917 please?

pistomat avatar Jun 13 '23 10:06 pistomat

The fix is adding this header in your request:

"Accept-Encoding": "gzip, deflate",

I looked closer at my request using Postman and noticed, that response headers includes Content-Encoding: gzip. I don't understand why it started causing issues out of nowhere, but your suggestion of adding the Accept-Encoding": "gzip request header helped me resolve this issue. Thank you @louislam !

MarkoOndrejicka avatar Aug 24 '23 09:08 MarkoOndrejicka

I recently fixed the issue. In our cases, it was usually related to WordPress with a cache plugin. If you try curl https://your-domain, the response is broken too usually.

The fix is adding this header in your request:

"Accept-Encoding": "gzip, deflate",

You can read this for more info: louislam/uptime-kuma#2253

Commit: louislam/uptime-kuma@3e68cf2

Adding -> "Accept-Encoding": "gzip, deflate" as my headers fixed the issue for me Thank you @louislam

I made the request on the backend to another server and I increased the content size up to 10mb, but still got the same error when my response was around 2 MB.

Didn't dig too deep into why it happens, but I just slightly skimmed over the "Check of running Websites results in error "maxContentLength size of -1 exceeded"" issue referred by @louislam

zhandosm avatar Sep 21 '23 02:09 zhandosm

Best fix I found is to use got instead since this library clearly isn't maintained

colesiegel avatar Sep 21 '23 12:09 colesiegel

This cost me several hours, I finally narrowed down the problem for my case: The issue was the transfer-encoding. If set to chunked axios would throw the above error (on chunked there is no content-length, because its dynamic). The underlying reason looks to be a closed socket in the node http adapter.

Parts of this might be due to my specific setup. I have Hoverfly as a mitm proxy between node and the real system. The real upstream system would return responses without chunked encoding, but Hoverfly would replay the request with chunked encoding.

So the request would work live, but not when replayed. It worked fine for a long time, the issue only appeared when upgrading from [email protected] to [email protected].

Now, I'm just speculating, but when comparing the original response and the replayed one I noticed a few things. Maybe the reason for the closed socket is an error in node itself, because the replay would work fine for curl & Postman. In my specific example the data is interleaved with the connection being closed, I don't know enough about the standard to judge whether its the correct behavior, but I captured this with curl -vvv:

* TLSv1.2 (IN), TLS header, Supplemental data (23):
<Lots of JSON payload here...>* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* Closing connection 0
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.3 (OUT), TLS alert, close notify (256):<...Continued JSON payload>

So there is still some data arriving after the TLS connection has been closed. This is not the case without chunked encoding. Could be completely unrelated, but maybe it saves someone else some time.

johenning avatar Nov 22 '23 14:11 johenning

AxiosError: maxContentLength size of Infinity exceeded

this is still happening to me for no reason... I did set infinity and no matter what it will always crash. This must be some nasty bug in axois.

Crashing here : AxiosError: maxContentLength size of Infinity exceeded at IncomingMessage.handlerStreamAborted (D:\Private\Projects\NodeJS\MongoDB-Test\node_modules\axios\dist\node\axios.cjs:3034:23)

version "axios": "^1.6.3",

michalss avatar Dec 31 '23 09:12 michalss

I faced the same problem, adding the "Connection":"keep-alive" request header can resolve this problem.

Bigzo avatar Mar 07 '24 08:03 Bigzo

I get same issue.

wenshan avatar Mar 10 '24 11:03 wenshan