You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix JSON serialization for falsy request bodies - #658
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.
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.
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
[#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
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ofetchskipped body preparation whenever a payload was falsy, so numeric0and booleanfalsebypassed JSON serialization and the defaultapplication/jsoncontent type. Process any non-null, defined body for payload methods, and include numbers and booleans in the TypeScript body type. Keepundefinedandnullas no body.Regression coverage checks the encoded body and content type for
0andfalse, and verifies thatnullstill receives no default content type.Validation:
pnpm testpassed (lint and all 28 tests);pnpm buildandtsc --noEmitpassed. 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
0,false, or an empty string are now processed instead of being skipped.nullbodies do not add a content-type header.