fix: send empty JSON body on POST requests without a body - #19
Conversation
The better-auth server always calls JSON.parse() on the request body
for POST endpoints. When BetterAuthSwift sends a POST with no body
(e.g. sign-out), the server receives an empty string and
JSON.parse("") throws, causing a 500 Internal Server Error.
Send `{}` as the body for POST requests that have no explicit body,
matching the server's expectation of a valid JSON payload.
Fixes ouwargui#18
WalkthroughAdds a fallback to Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment Tip You can generate walkthrough in a markdown collapsible section to save space.Enable the |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
Sources/Core/Plugins/Middleware.swift (1)
59-63: LGTM! The fix correctly addresses the server JSON parsing issue.The implementation properly injects an empty JSON object for POST requests without a body, which resolves the
JSON.parse("")error on the server. The comment clearly explains the rationale.One minor consideration: the method comparison is case-sensitive. While HTTP methods should be uppercase per RFC 7230, and your codebase consistently uses
"POST", you could make this slightly more defensive:💡 Optional: Case-insensitive comparison
- } else if self.method == "POST" { + } else if self.method.uppercased() == "POST" {This is optional since all your route definitions already use uppercase consistently.
,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@Sources/Core/Plugins/Middleware.swift` around lines 59 - 63, The POST check is case-sensitive (self.method == "POST"); make it defensive by comparing case-insensitively (e.g., normalize self.method via uppercased()/lowercased() or use a case-insensitive comparison) before setting request.httpBody to Data("{}".utf8) in Middleware.swift so that the POST branch reliably triggers regardless of method casing while preserving the existing behavior of injecting an empty JSON body.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@Sources/Core/Plugins/Middleware.swift`:
- Around line 59-63: The POST check is case-sensitive (self.method == "POST");
make it defensive by comparing case-insensitively (e.g., normalize self.method
via uppercased()/lowercased() or use a case-insensitive comparison) before
setting request.httpBody to Data("{}".utf8) in Middleware.swift so that the POST
branch reliably triggers regardless of method casing while preserving the
existing behavior of injecting an empty JSON body.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4b7ff8d5-0eca-4262-9d19-ca7bd6059734
📒 Files selected for processing (1)
Sources/Core/Plugins/Middleware.swift
|
thx for the contribution |
Problem
When BetterAuthSwift sends a POST request without an explicit body (e.g.
signOut()), the request arrives at the better-auth server with an empty body. The server unconditionally callsJSON.parse()on the request body for POST endpoints, andJSON.parse("")throws aSyntaxError, resulting in a 500 Internal Server Error.Fix
Send
{}as the HTTP body for POST requests that have no explicit body set, matching the server's expectation of a valid JSON payload.Testing
client.signOut()against a better-auth server — previously returned 500, now returns 200 with{ "success": true }.Fixes #18
Summary by CodeRabbit
Bug Fixes