I’d like to write a simple middleware that checks whether the request’s Content-type is “application/json”. How would I go about it? The ServerRequestInterface
passed does not have the content type parsed. If I do my own parsing it might be error prone with all the valid things that can come after the MIME type. Any suggestions?
Update: apart from copying the Request
logic that Slim has on top of PSR7, I noticed it is actually possible to change the signature of the Middleware to use the Slim extended Request
interface.
class JSONContentMiddleware
{
public function __invoke(Request $request, Response $response, callable $next)
{
if ($request->getContentType() !== "application/json")
return $response->withStatus(400, "Content-Type should be application/json");
return $next($request, $response);
}
}
```
Thanks,
Achim