diff --git a/app/Http/Middleware/JsonOnlyMiddleware.php b/app/Http/Middleware/JsonOnlyMiddleware.php index 1cccf243..1188a226 100644 --- a/app/Http/Middleware/JsonOnlyMiddleware.php +++ b/app/Http/Middleware/JsonOnlyMiddleware.php @@ -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); diff --git a/tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php b/tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php index aedd2bfb..1aee383b 100644 --- a/tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php +++ b/tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php @@ -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.'] + ] + ]); + + } }