-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust handling of invalid API requests (#885)
- Loading branch information
Showing
7 changed files
with
114 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
|
||
class ApiHeaderValidationMiddleware | ||
{ | ||
/** | ||
* This middleware ensures that the Content-Type header is set to | ||
* application/json for POST, PATCH or DELETE, otherwise it will return a 415 Unsupported | ||
* Media Type response. | ||
* | ||
* @param Request $request | ||
* @param Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle(Request $request, Closure $next): mixed | ||
{ | ||
if (in_array($request->method(), ['GET', 'HEAD', 'OPTIONS'])) { | ||
return $next($request); | ||
} | ||
|
||
if ($request->header('Content-Type') !== 'application/json') { | ||
return response()->json([ | ||
'error' => 'Invalid Content-Type header, LinkAce only supports JSON input' | ||
], 415); | ||
} | ||
|
||
if ($request->header('Accept') !== 'application/json') { | ||
return response()->json([ | ||
'error' => 'Invalid Accept header, LinkAce only supports JSON output' | ||
], 415); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
app/Http/Middleware/ContentTypeHeaderValidationMiddleware.php
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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