Skip to content

Fixes #7494 Fetch Adapter: Content length header is missing in POST request#10633

Closed
Kilros0817 wants to merge 5 commits intoaxios:v1.xfrom
Kilros0817:my-fix-branch
Closed

Fixes #7494 Fetch Adapter: Content length header is missing in POST request#10633
Kilros0817 wants to merge 5 commits intoaxios:v1.xfrom
Kilros0817:my-fix-branch

Conversation

@Kilros0817
Copy link
Copy Markdown
Contributor

@Kilros0817 Kilros0817 commented Apr 2, 2026

Description

Fixes inconsistency between the fetch adapter and the default http adapter in Node.js, where several headers are not automatically included.

Issues #7494

Problem

When using Axios fetch adapter in Node.js (v1.13.6), the following headers are not automatically set, unlike the default http adapter:

User-Agent
Content-Length
Accept-Encoding
Host
Connection

This leads to inconsistent behavior between adapters and may cause issues with servers that rely on these headers.

Solution

This PR updates the fetch adapter to align with the default http adapter behavior in Node.js:

Adds User-Agent: axios/ if not already provided
Adds Accept-Encoding: gzip, compress, deflate, br if not already provided
Computes and sets Content-Length when possible

Notes

Host and Connection headers are intentionally not set
These are controlled internally by the fetch implementation
Manually setting them is either ignored or disallowed (forbidden headers)

Testing

Verified headers are correctly added when using the fetch adapter in Node.js
Confirmed no override occurs if headers are explicitly provided by the user


Summary by cubic

Aligns the Node.js fetch adapter with the Node http adapter by auto-setting User-Agent, Accept-Encoding, and computing Content-Length when safe. Fixes #7494 to prevent missing headers in POST requests; no browser changes.

Description

  • Node-only defaults in lib/adapters/fetch.js using VERSION.
  • Set User-Agent: axios/<version> and Accept-Encoding: gzip, compress, deflate, br if missing.
  • Compute Content-Length for measurable bodies (Blob, ArrayBuffer/View, string, URLSearchParams); skip FormData/streams.
  • Do not override user headers; do not set Host or Connection (managed by fetch).
  • Keeps behavior consistent with the Node http adapter.
  • Rebased on latest v1.x; no behavior change.

Docs

  • Node-only behavior; consider noting default headers for the fetch adapter in Node.
  • No changes needed for browsers.

Testing

  • No tests added.
  • Suggested Node tests:
    • Defaults applied when absent; headers not overridden if provided.
    • Content-Length set for measurable bodies; skipped for FormData/streams.
    • Guard that browser builds do not include these defaults.

Written for commit c61f3a5. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file

Confidence score: 3/5

  • There is some risk of user-impacting regressions because lib/adapters/fetch.js now prebuffers FormData to compute Content-Length, which can significantly increase memory use and latency on large uploads.
  • Severity is medium (6/10) with a clear performance impact path, so this is more than a minor housekeeping change.
  • Pay close attention to lib/adapters/fetch.js - prebuffering FormData may cause memory/latency regressions on large uploads.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="lib/adapters/fetch.js">

<violation number="1" location="lib/adapters/fetch.js:179">
P2: Node fetch path now prebuffers spec-compliant FormData to compute Content-Length, risking major memory/latency regressions on large uploads.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread lib/adapters/fetch.js Outdated
@Kilros0817
Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai Could you please read this comment?
The current implementation does not prebuffer FormData. Content-Length is only computed for body types that can be measured without consuming the body: Blob (via .size), ArrayBuffer/views (via .byteLength), strings, and URLSearchParams. FormData and streams are explicitly skipped — no buffering occurs for those types. The comment in the code documents this: // FormData and streams cannot be re-read.
Thanks.

@cubic-dev-ai
Copy link
Copy Markdown
Contributor

cubic-dev-ai Bot commented Apr 3, 2026

@cubic-dev-ai Could you please read this comment?
The current implementation does not prebuffer FormData. Content-Length is only computed for body types that can be measured without consuming the body: Blob (via .size), ArrayBuffer/views (via .byteLength), strings, and URLSearchParams. FormData and streams are explicitly skipped — no buffering occurs for those types. The comment in the code documents this: // FormData and streams cannot be re-read.
Thanks.

@Kilros0817 You're right, and I appreciate the clarification. Looking closely at the implementation in lib/adapters/fetch.js, I see that you've explicitly restricted the Content-Length calculation to types with a known size or that can be safely encoded:

  • Blob: Uses data.size.
  • ArrayBuffer/View: Uses data.byteLength.
  • Strings/URLSearchParams: Uses encodeText.

The logic correctly omits FormData and streams, which avoids the risk of prematurely consuming the body or causing performance hits from pre-buffering. This approach safely aligns the fetch adapter's behavior with the Node http adapter for measurable payloads while respecting the limitations of streamable data.

For more details on how these rules are applied, you can check the Axios wiki.

@jasonsaayman
Copy link
Copy Markdown
Member

@Kilros0817 can we maybe add some testing here?

@jasonsaayman jasonsaayman added commit::fix The PR is related to a bugfix status::changes-requested A reviewer requested changes to the PR labels Apr 4, 2026
@jasonsaayman jasonsaayman self-requested a review April 4, 2026 17:16
Copy link
Copy Markdown
Collaborator

@DigitalBrainJS DigitalBrainJS left a comment

Choose a reason for hiding this comment

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

Content-Length and Accept-Encoding are set automatically by fetch. Besides, we can't be sure what compression algorithms are actually supported by fetch on each platform, so we shouldn't hardcode it.
Setting User-Agent is a valid change, as by default it sets it to 'node'.

@jasonsaayman
Copy link
Copy Markdown
Member

jasonsaayman commented Apr 4, 2026

Thanks @DigitalBrainJS I think we can close this @Kilros0817 please can you open a new PR with only the User-Agent change and tests if possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit::fix The PR is related to a bugfix status::changes-requested A reviewer requested changes to the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fetch Adapter: Content length header is missing in POST request

3 participants