Skip to content

fix(fetch): send native FormData as multipart/form-data in Node.js - #11351

Open
spokodev wants to merge 6 commits into
axios:v1.xfrom
spokodev:fix/fetch-native-formdata-content-type
Open

spokodev wants to merge 6 commits into
axios:v1.xfrom
spokodev:fix/fetch-native-formdata-content-type

Conversation

@spokodev

@spokodev spokodev commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In Node.js, axios.post(url, formData, { adapter: 'fetch' }) with a native FormData sends Content-Type: application/x-www-form-urlencoded while fetch writes a multipart body, so the server cannot parse the form. The http adapter sends multipart/form-data; boundary=... for the same call.

dispatchRequest sets application/x-www-form-urlencoded as the default Content-Type for POST, PUT and PATCH. resolveConfig clears it for FormData only in browser-like environments, and the fetch adapter only removed a multipart/form-data type without a boundary, so in Node.js the default reached the request. This change makes the fetch adapter remove any Content-Type on a FormData body except multipart/form-data with a boundary, so fetch sets the header with the boundary it generates.

Linked issue

Related to #6993, which reported this symptom and was closed after #6998. #6998 handled form-data package objects (getHeaders()); native FormData in Node.js still sends the urlencoded type on 1.12.0, 1.20.0 and v1.x.

Changes

  • lib/adapters/fetch.js: for FormData bodies, keep only a multipart/form-data Content-Type that has a boundary.
  • tests/unit/adapters/fetch.test.js: native FormData with no Content-Type (post, put, patch) and with an explicit urlencoded Content-Type both arrive as multipart/form-data; boundary=....
  • PRE_RELEASE_CHANGELOG.md: Bug Fixes entry.

Validation

Node.js v26.0.0, macOS arm64.

  • npm ci --ignore-scripts
  • npm run lint: passed.
  • npm run build: passed.
  • npm run test:vitest:unit: 74 files, 1702 tests passed. Without the fetch.js change the two new tests fail and the other 83 tests in fetch.test.js pass.
  • npm run test:vitest:browser:headless: 81 files, 1065 passed, 3 skipped (Playwright 1.64.0, Chromium, Firefox, WebKit).
  • Manual check against a local server that parses the body with Response.formData(): on 1.20.0 the fetch adapter sends application/x-www-form-urlencoded and the body does not parse; with this change it sends multipart/form-data; boundary=... and the server reads name and file, the same as the http adapter.
  • GitHub Actions status: to be recorded after the run.

Compatibility and documentation

Patch. Affects the fetch adapter outside browsers when the body is a native FormData. A non-multipart Content-Type set explicitly for a FormData body is now replaced, as the http adapter and axios in browsers already do; before this change such a request could not be parsed. A multipart/form-data type with a boundary, such as one from form-data's getHeaders(), is kept. No index.d.ts/index.d.cts or PRE_RELEASE_DOCS.md changes are needed.

Not covered here: a form-data package instance or postForm() with a plain object through the fetch adapter in Node.js still sends [object FormData] as the body. That is a separate problem.

Checklist

  • Scope, target branch, and linked issue/rationale checked
  • PRE_RELEASE_CHANGELOG.md entry added or updated (or AGENTS.md's automated release-preparation exception explained)
  • Regression tests and relevant negative/platform cases covered (or N/A with reason)
  • Applicable local checks completed, with actual commands and results recorded above
  • Deferred release docs and ESM/CJS types/tests updated where needed (or N/A with reason)
  • Semver impact stated and intentional breaking changes explained
  • Final diff reviewed for security, unrelated changes, generated artifacts, and test/debug leftovers
  • Latest GitHub Actions status recorded separately from review/security bot results

AI assistance: Claude Code assisted with the investigation, the change, the tests and this description; I reviewed and ran everything above. I can't apply labels, so a maintainer needs to apply ai-assisted.


Summary by cubic

Description

Fixes the fetch adapter sending native FormData bodies with Content-Type: application/x-www-form-urlencoded in Node.js, which caused servers to receive an unparseable body since fetch actually serializes multipart data. The fetch adapter now removes any Content-Type on a FormData body that is not multipart/form-data with a boundary, letting fetch set the header and boundary it generates. This matches the behavior of the Node HTTP adapter and browser environments.

Docs

No documentation update needed; no public API or type changes.

Testing

Added two tests: native FormData with no Content-Type (POST, PUT, PATCH) and with an explicit urlencoded Content-Type both arrive as multipart/form-data; boundary=.... Each test parses the body back on the receiving server, so a header whose boundary does not match the body also fails. Unit and headless browser suites pass.

Semantic version impact

Patch. Only the fetch adapter behavior outside browsers changes for FormData bodies; no breaking changes, and an explicitly set non-multipart Content-Type on a FormData body is now replaced, as the http adapter and browsers already do.

Written for commit 0d7af1f. Summary will update on new commits.

View guided diff

The fetch adapter only dropped a multipart/form-data Content-Type
without a boundary, so in Node.js a native FormData body kept the
application/x-www-form-urlencoded default while fetch sent a multipart
body. Drop any Content-Type except multipart/form-data with a boundary
so fetch sets the header with its own boundary.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable defects were found.

Summary

Fixes native FormData uploads through the fetch adapter in Node.js.

  • Node.js fetch requests with native FormData use a matching multipart header.

No actionable issues found. Review was based on source inspection; no tests were run during this review.

Reviews (8) · Last reviewed commit: "Merge remote-tracking branch 'upstream/v..." · Reviewed by Greptile

@jasonsaayman jasonsaayman added ai-assisted This PR was created with or in conjunction with AI commit::fix The PR is related to a bugfix v1x Version 1 labels Oct 21, 2026

@jasonsaayman jasonsaayman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PR #11351 - fix(fetch): send native FormData as multipart/form-data in Node.js

Repo: axios/axios Author: @spokodev Base: v1.x Files: 3 **+48/-3
CI: 4 success; no build/test workflow result is present in this snapshot. Mergeability: mergeable. Reviewed head: 88bd9aedaa28f0dd468a39420dd818081027816e.

Repo Profile

Axios is an npm JavaScript HTTP client for browsers and Node, with XHR/Fetch/HTTP adapters, default and named exports, interceptors, headers/errors, and paired ESM/CJS declarations. The target uses Vitest plus browser and package/module tests; v1 packaging covers older Node CJS runtimes as well as current ESM, Bun and Deno. Contributions preserve transport/security boundaries, add regression coverage and prerelease records, and defer unreleased user docs to PRE_RELEASE_DOCS.md. This review uses the actual target-branch guidance and files, not the separate local v0 checkout.

Verdict

Request changes — No concrete source defect found in this review; the missing latest-head validation prevent merge approval.
Posted as a comment review for the validation/integration hold; no formal code-change request is implied beyond the specific items below.

Change Explanation And User Impact

Native FormData reaches the Fetch adapter after dispatchRequest has installed the ordinary POST/PUT/PATCH content-type default. Fetch generates multipart bytes, but the existing non-multipart header prevents the runtime from supplying their matching boundary. This change removes that conflicting header for the native FormData path while preserving explicit multipart boundaries handled by the serializer. Executed against a local HTTP receiver: axios.post(url, form, {adapter: "fetch"}) with foo=bar previously arrived as application/x-www-form-urlencoded and parsed as a malformed field; this head arrived as multipart/form-data with a boundary and parsed as foo=bar. Requests using upload tracking, a body limit, or an explicit boundary also need the existing serialized-Blob path to remain consistent.

Validation scope: source/diff and regression-test review. Examples explicitly marked executed used isolated source snapshots on Node 24.18.0 with the workspace dependency installation; these were focused checks, not a clean install, full CI matrix, or full test-suite run. Author-reported test results were not treated as independently executed.

Competing PRs

Search: axios/axios open PRs, including drafts; full open-list scan plus FormData multipart in:title,body, filename in:title,body. Complementary searches were run across related APIs/issues; no query reached the 100-result cap and no search failed. The five explicitly excluded PRs were not reviewed or modified in this batch.

#11351 fixes native Fetch multipart headers; #11332 preserves empty filenames in the Node HTTP serializer. They operate at different boundaries and are complementary.

Preferred PR: No competing implementation supersedes this PR; continue it independently.

Unique contributions: The submitted regression cases protect this specific boundary; preserve them independently of adjacent fixes.

Action: No duplicate closure: the candidates are complementary or no equivalent solution was found.

Project Fit

North star: Reliable HTTP transport and configuration across supported JavaScript environments, without embedding application-specific policy.

Benefits: Makes Fetch multipart headers match the actual body boundary.

Tradeoffs: A caller-supplied non-multipart header is now replaced for a FormData body; that makes the wire representation coherent, but should be explicit in the release note.

Fit: Strong — The underlying problem belongs in the transport, serializer, declarations or their maintenance tests.

Adversarial Review

Strongest objection: A caller-supplied non-multipart header is now replaced for a FormData body; that makes the wire representation coherent, but should be explicit in the release note. Latest-head integration evidence is still needed.

Abuse/failure modes checked: Traced native versus serialized multipart paths, explicit boundaries, progress/body-limit selection, and the new post/put/patch header assertions. The local receiver also parsed the body successfully.

Result: No concrete source defect established in the examined cases; validation and integration limits remain below.

Disposition

Continue
Address the specific blockers below; no unrelated scope or complementary PR should be closed.

Semver

patch — Corrects existing behavior or strengthens internal tooling/tests without adding a public API.

Breaking Changes

None intended beyond the bug correction described above; no public API removal or new runtime requirement is introduced by this diff.

Documentation

PRE_RELEASE_CHANGELOG.md records the change. No new API reference or migration is required for this scoped correction; keep the release description consistent with the behavior and limits above.

Performance And Regression Risk

Hot path touched: Yes — request/response processing or its shared helpers.

Performance concern: No unbounded new work identified in the changed path. A caller-supplied non-multipart header is now replaced for a FormData body; that makes the wire representation coherent, but should be explicit in the release note.

Regression risk: low — the changed boundary needs regression and integration coverage, particularly after any rebase.

Evidence requested: The latest-head checks listed below; no extra benchmark requested for this scoped change.

Blocking Issues

  • Latest-head build/test validation is unavailable in the returned GitHub checks. Run or attach the repository build, relevant regression suite and required package/type/browser checks before merge; review/security bots alone do not establish that they pass. This is a validation hold, not an invented source defect.

Non-Blocking Suggestions

  • Add the receiver-side formData() assertion to the committed regression, so header and body boundary agreement are verified together.

Looks Good

  • Traced native versus serialized multipart paths, explicit boundaries, progress/body-limit selection, and the new post/put/patch header assertions. The local receiver also parsed the body successfully.

Assert that the server can read the field back with the Content-Type it
received, so a header whose boundary does not match the body also fails.
@spokodev

Copy link
Copy Markdown
Contributor Author

The build and test workflows have not run on this branch yet: Continuous integration, Verify build reproducibility, Bundle Size and zizmor are waiting for approval (action_required) and need "Approve and run workflows". Locally on Node 26, the version CI uses, npm run lint, npm run build and npm run test:vitest:unit (1702 tests) pass on 58f6657.

I added the receiver-side check in 58f6657. The test server now parses the body with Response.formData() using the Content-Type it received, and the tests assert the field value as well as the header. A Content-Type whose boundary does not match the body now fails them with "Failed to parse body as FormData."; the previous version of the tests passed in that case.

The PRE_RELEASE_CHANGELOG entry already covers a caller-set non-multipart Content-Type being replaced.

@spokodev

Copy link
Copy Markdown
Contributor Author

I merged v1.x again. The only conflict was in PRE_RELEASE_CHANGELOG.md.

The CI workflows have not run on this branch yet and are waiting for "Approve and run workflows". Locally on Node 26, on this head, npm run lint, npm run build, npm run test:vitest:unit (1783 tests) and npm run test:vitest:browser:headless (1122 passed, 3 skipped) pass. With lib/adapters/fetch.js from v1.x, the two new tests fail.

This branch has not been deployed

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

Labels

ai-assisted This PR was created with or in conjunction with AI commit::fix The PR is related to a bugfix v1x Version 1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants