Skip to content

fix: return 400 for invalid JSON request bodies#1722

Open
Omar-Tnt04 wants to merge 1 commit intotypicode:mainfrom
Omar-Tnt04:fix-invalid-body-status
Open

fix: return 400 for invalid JSON request bodies#1722
Omar-Tnt04 wants to merge 1 commit intotypicode:mainfrom
Omar-Tnt04:fix-invalid-body-status

Conversation

@Omar-Tnt04
Copy link
Contributor

Summary

Return 400 Bad Request instead of 404 Not Found when POST/PUT/PATCH requests receive an invalid non-object JSON body.

What changed

  • Updated withBody in src/app.ts
  • Updated withIdAndBody in src/app.ts
  • Added tests in src/app.test.ts for invalid request bodies

Why

Previously, invalid bodies such as arrays, strings, or null would fall through to the final handler and return 404 Not Found, which is misleading.

A malformed request body should return 400 Bad Request.

Tests

All tests pass, including new coverage for:

  • POST with array body → 400
  • PATCH with string body → 400
  • PUT with null body → 400

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