Skip to content

Fix JSON serialization for falsy request bodies - #658

Open
2836048681 wants to merge 1 commit into
unjs:mainfrom
2836048681:fix/645-serialize-falsy-json-bodies
Open

2836048681 wants to merge 1 commit into
unjs:mainfrom
2836048681:fix/645-serialize-falsy-json-bodies

Conversation

@2836048681

@2836048681 2836048681 commented Nov 21, 2026 •

Copy link
Copy Markdown

ofetch skipped body preparation whenever a payload was falsy, so numeric 0 and boolean false bypassed JSON serialization and the default application/json content type. Process any non-null, defined body for payload methods, and include numbers and booleans in the TypeScript body type. Keep undefined and null as no body.

Regression coverage checks the encoded body and content type for 0 and false, and verifies that null still receives no default content type.

Validation: pnpm test passed (lint and all 28 tests); pnpm build and tsc --noEmit passed. Tested with Node.js 24.22.1 and pnpm 10.20.0.

Prepared locally only. No push or pull request was made.

Fixes #645.

Summary by CodeRabbit

  • Bug Fixes
    • Request bodies containing 0, false, or an empty string are now processed instead of being skipped.
    • null bodies do not add a content-type header.
  • New Features
    • Request options now accept numeric and boolean bodies, with falsy JSON values serialized as JSON when applicable.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

Payload methods now process request bodies other than undefined and null, including falsy values. The public body type accepts numbers and booleans. Tests cover JSON serialization of 0 and false, and verify that null does not add a content-type header.

Changes: Request Body Handling

Layer / File(s) Summary
Accept and serialize falsy bodies
src/types.ts, src/fetch.ts, test/index.test.ts
FetchOptions.body now accepts numbers and booleans. Payload methods process bodies other than undefined and null. Tests verify that 0 and false are sent as JSON and that null does not add a content-type header.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • unjs/ofetch#646: Changes the same request-body preparation behavior for falsy JSON values.

Suggested reviewers: pi0, bounoable

Merge Risk: Low · up to 18228

Falsy numeric and boolean bodies are now sent as JSON, which is the intended fix. Empty-string bodies will now also be sent with an application/json content type instead of text/plain. Endpoints that accept text but reject JSON could notice this. The impact is narrow, and the owner should confirm it is intended before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Fix JSON serialization for falsy request bodies" matches the main change. The raw summary and objectives show that payload-method bodies such as 0 and false are now JSON-serialized with the…
Linked Issues check Passed [#645] Requires that numeric 0 and boolean false bodies are JSON-serialized with content-type: application/json, while undefined and null remain no-body values. At head, src/fetch.ts repla…
Out of Scope Changes check Passed The changes are limited to src/fetch.ts (body-preparation guard), src/types.ts (body type), and test/index.test.ts (regression tests). Each change serves #645. One side effect follows from the n…
Full details: Title check

Explanation

The title "Fix JSON serialization for falsy request bodies" matches the main change. The raw summary and objectives show that payload-method bodies such as 0 and false are now JSON-serialized with the application/json content type, and that undefined and null stay no-body values. The title names the primary fix and does not list files. It is specific and concise.

Full details: Linked Issues check

Explanation

[#645] Requires that numeric 0 and boolean false bodies are JSON-serialized with content-type: application/json, while undefined and null remain no-body values. At head, src/fetch.ts replaces the truthy check with context.options.body !== undefined && context.options.body !== null && isPayloadMethod(...). Falsy values now enter the existing isJSONSerializable branch. isJSONSerializable in src/utils.ts already returns true for number and boolean. That branch stringifies non-string bodies with JSON.stringify and sets content-type: application/json when no content type is present. src/types.ts widens FetchOptions.body to include number | boolean. The PR summary reports regression tests for 0, false, and null in test/index.test.ts. I did not read the test file, so the test coverage claim rests on the PR summary. The implementation meets the stated requirements.

Full details: Out of Scope Changes check

Explanation

The changes are limited to src/fetch.ts (body-preparation guard), src/types.ts (body type), and test/index.test.ts (regression tests). Each change serves #645. One side effect follows from the new non-null rule: an empty string body "" now enters the JSON branch. It receives content-type: application/json and accept: application/json, where it previously had neither. This follows the PR's stated rule that any defined, non-null body is processed. It is not a separate unrelated change. The reviewer may still want to confirm this empty-string behavior is intended. The test file was not inspected directly.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

ESLint analysis is incomplete because its dependency install exceeded the time available for this review.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Additional comments: 2

src/fetch.ts-133-133: 1

133-133: Minor · Data Integrity & Integration · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Use the effective method for Request inputs.

For a POST Request passed with { body: false } and no options.method, isPayloadMethod receives undefined and defaults to GET. The body skips serialization, while native fetch retains the Request’s POST method and converts the body to text. (fetch.spec.whatwg.org) Use the Request method when no options method overrides it, so this supported payload path also gets the JSON content type.

src/fetch.ts-131-132: 1

131-132: Major · Data Integrity & Integration · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Preserve empty-string body handling.

A POST with body: "" now enters the JSON branch. The branch sends an empty body with content-type: application/json, although an empty body is not a JSON document. A server that parses JSON or requires text content can reject requests that previously used the native text content type. Keep empty strings on the previous path while processing 0 and false.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 80748139-4153-47d7-a4ce-a7675c3956e2
Commits

Reviewing files that changed from the base of the PR and between 1dbc37f and 1822825.

Files selected for processing: 3

  • src/fetch.ts
  • src/types.ts
  • test/index.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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.

Falsy request bodies are sent as text/plain instead of application/json

1 participant