Skip to content

fix(fetch): send form-data package bodies instead of "[object FormData]" - #11509

Open
r0h1tb wants to merge 2 commits into
axios:v1.xfrom
r0h1tb:fix/fetch-form-data-package
Open

r0h1tb wants to merge 2 commits into
axios:v1.xfrom
r0h1tb:fix/fetch-form-data-package

Conversation

@r0h1tb

@r0h1tb r0h1tb commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In Node.js, the fetch adapter sends a form-data package object as the string [object FormData]. The Content-Type is multipart/form-data with the boundary from getHeaders(), so the server cannot parse the body.

This is the object axios builds itself when env.FormData is the default form-data package, so it affects postForm(), putForm(), patchForm() and a plain object sent with Content-Type: multipart/form-data:

await axios.postForm(url, { name: 'x' }, { adapter: 'fetch' });
// server receives: Content-Type: multipart/form-data; boundary=----...  body: [object FormData]

The Node HTTP adapter sends the form correctly. The fetch adapter now sends the form serialized by the object's getBuffer(), which matches that boundary. Because the body is now a Buffer, maxBodyLength and onUploadProgress also see its real size, and a form whose known length is over maxBodyLength is rejected before getBuffer() copies it. getBuffer() cannot serialize stream values, such as fs.createReadStream(), so a form with them (no known length) rejects with ERR_NOT_SUPPORT instead of sending the string. Any other getBuffer() failure is wrapped with AxiosError.from(), keeping the original error as cause. Spec-compliant FormData is not affected.

Linked issue

No issue. #6998 (for #6993) set the Content-Type of form-data objects in the fetch adapter, and its test only checks that header. The body was never sent. #11351 covers native FormData in Node.js getting the urlencoded type, which is a separate bug.

Changes

  • lib/adapters/fetch.js: for a non-spec-compliant FormData with getBuffer(), reject stream values and an oversized known length first, then send getBuffer().
  • tests/unit/adapters/fetch.test.js: the server parses the received body with Response.formData(), for a form-data object with a string and a file field and for postForm() with an object. A form with a stream value rejects with ERR_NOT_SUPPORT, an oversized form rejects with ERR_BAD_REQUEST without calling getBuffer(), and a getBuffer() failure keeps its cause.
  • PRE_RELEASE_CHANGELOG.md: Bug Fixes entry.

Validation

Node.js v22.23.2, macOS arm64, npm ci --ignore-scripts.

  • Without the change, the two body tests fail because the server gets [object FormData], and the stream test fails because the request succeeds. The size-limit and cause tests fail on the first commit of this PR.
  • npm run lint: passes.
  • npm run build: passes.
  • npm run test:vitest:unit: 1804 passed, 1 failed. The failing test, should not use inherited Symbol.iterator for request headers, fails the same way on v1.x without this change on this Node version (1799 passed, 1 failed there).
  • npm run test:vitest:browser:headless (chromium, firefox, webkit): 1131 passed, 3 skipped, on the first commit. The second commit only changes the branch for objects with getBuffer(), which browsers do not reach.
  • I also checked by hand against a local server: postForm, putForm and patchForm send the fields, maxBodyLength: 10 rejects with ERR_BAD_REQUEST, and onUploadProgress reports the body length.

GitHub Actions status will be recorded below once the runs are approved.

Compatibility and documentation

Patch. Only Node.js with the fetch adapter is affected. A form-data object with stream values used to send [object FormData] and now rejects with ERR_NOT_SUPPORT. No public API or type changes, so no index.d.ts/index.d.cts or PRE_RELEASE_DOCS.md update.

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 was used for this change and description. I could not apply the ai-assisted label (no permission), so a maintainer needs to apply it.

In Node.js, axios builds multipart bodies for postForm(), putForm(),
patchForm() and plain objects sent as multipart/form-data with the
form-data package. Its objects are Node.js streams, which fetch sent as
the string "[object FormData]" under the multipart Content-Type set from
getHeaders(), so servers could not parse the form.

Send the form serialized by getBuffer() instead, so the body matches the
boundary and maxBodyLength and onUploadProgress see its real size. A
form with stream values cannot be serialized that way and now rejects
with ERR_NOT_SUPPORT.
@r0h1tb
r0h1tb requested a review from jasonsaayman as a code owner November 25, 2026 19:11
@greptile-apps

greptile-apps Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

The fetch adapter now sends serialized form-data bodies instead of [object FormData].

  • Node.js fetch requests send form-data package bodies as multipart data.

The previous finding about copying oversized forms is addressed. No new actionable issues were found. Checks were not rerun during this review.

Reviews (3) · Last reviewed commit: "fix(fetch): check maxBodyLength before s..." · Reviewed by Greptile

Comment thread lib/adapters/fetch.js Resolved

@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.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread lib/adapters/fetch.js Resolved
Comment thread lib/adapters/fetch.js Resolved
Compare the form's known length with maxBodyLength before getBuffer()
copies it, report stream values from hasKnownLength(), and keep the
original error as the cause for any other getBuffer() failure.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant