Skip to content

Commit

Permalink
fix: allow empty body (#59)
Browse files Browse the repository at this point in the history
Enable acceptance of empty body requests in the API when sent to an
endpoint. This adjustment ensures compatibility with the usual JSON body
allowance.

---------

Signed-off-by: Valentin Sickert <[email protected]>
  • Loading branch information
Lapotor authored Dec 11, 2023
1 parent fcb87c7 commit 3b95567
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/Http/Middleware/JsonOnlyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JsonOnlyMiddleware
*/
public function handle(Request $request, Closure $next): Response
{
if (!$request->isJson()) {
if (!empty($request->all()) && !$request->isJson()) {
return response()->json([
'message' => 'Only JSON requests are accepted'
], Response::HTTP_BAD_REQUEST);
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,23 @@ public function test_json_requests_are_accepted(): void

$user->delete();
}

/**
* Test that empty requests are accepted.
*/
public function test_empty_requests_are_accepted(): void
{

$response = $this->post(uri: '/api/v1/login', headers: ['Accept' => 'application/json']);

$response->assertStatus(422)
->assertJson([
'message' => 'The email field is required. (and 1 more error)',
'errors' => [
'email' => ['The email field is required.'],
'password' => ['The password field is required.']
]
]);

}
}

0 comments on commit 3b95567

Please sign in to comment.