Skip to content

fix: Pages API body parser for invalid JSON and repeated form keys#446

Open
JaredStowell wants to merge 3 commits intocloudflare:mainfrom
JaredStowell:jstowell/fix-pages-api-body-parser-parity
Open

fix: Pages API body parser for invalid JSON and repeated form keys#446
JaredStowell wants to merge 3 commits intocloudflare:mainfrom
JaredStowell:jstowell/fix-pages-api-body-parser-parity

Conversation

@JaredStowell
Copy link
Contributor

Bring Pages API body parsing in line with Next.js for JSON and urlencoded requests.

Changes

  • return 400 Invalid JSON for malformed application/json requests instead of passing raw strings to handlers
  • preserve duplicate application/x-www-form-urlencoded keys as arrays via querystring.decode()
  • treat empty JSON bodies as {} to match Next.js behavior
  • recognize application/ld+json as JSON
  • avoid logging/reporting invalid JSON as a server error
  • preserve response statusText through the Pages production server path
  • add dev and production regression coverage for all of the above

Testing

  • pnpm test tests/api-handler.test.ts tests/pages-router.test.ts tests/entry-templates.test.ts -u
  • pnpm test tests/features.test.ts -t "production server compression"
  • pnpm run fmt
  • pnpm run typecheck

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 11, 2026

Open in StackBlitz

npm i https://pkg.pr.new/vinext@446

commit: 78ce4e5

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d8ceba9cf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1031 to +1034
middlewareRewriteStatus ?? response.status,
responseHeaders,
compress,
response.statusText || undefined,

Choose a reason for hiding this comment

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

P2 Badge Drop stale status text when rewrite overrides status

When middlewareRewriteStatus overrides the downstream response status, this call still forwards response.statusText, so the reason phrase can describe a different status code than the one actually sent (for example, a middleware rewrite to 403 combined with an API response that set statusText to Invalid JSON yields 403 Invalid JSON). This mismatch was introduced by propagating statusText and affects any rewritten API/page response with a custom status text; only preserve response.statusText when the final status code is unchanged.

Useful? React with 👍 / 👎.

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