/** * Check request content-type header to require JSON for methods with payloads. * * @param Request $request * @throws Exception\UnsupportedMediaTypeException */ protected function checkContentType(Request $request) { // Require application/json Content-Type for certain methods. $method = strtolower($request->getMethod()); $contentType = $request->getHeader('content-type'); if (in_array($method, ['post', 'put', 'patch']) && !$contentType->match(['application/json', 'multipart/form-data'])) { $contentType = $request->getHeader('Content-Type'); $errorMessage = sprintf('Invalid Content-Type header. Expecting "application/json", got "%s".', $contentType ? $contentType->getMediaType() : 'none'); throw new Exception\UnsupportedMediaTypeException($errorMessage); } }