unsupportedMediaType() public static method

Is thrown when incorrect (or unknown) Content-Type was provided in request
public static unsupportedMediaType ( string $message = '', Exception | Throwable $previous = NULL ) : BadRequestException
$message string
$previous Exception | Throwable
return BadRequestException
Exemplo n.º 1
0
 /**
  * Parse request body if any
  * @return array|\Traversable
  *
  * @throws BadRequestException
  */
 protected function parseRequestBody()
 {
     $requestBody = array();
     $input = class_exists('Nette\\Framework') && Nette\Framework::VERSION_ID <= 20200 ? file_get_contents('php://input') : $this->httpRequest->getRawBody();
     if ($input) {
         try {
             $this->mapper = $this->mapperContext->getMapper($this->httpRequest->getHeader('Content-Type'));
             $requestBody = $this->mapper->parse($input);
         } catch (InvalidStateException $e) {
             throw BadRequestException::unsupportedMediaType('No mapper defined for Content-Type ' . $this->httpRequest->getHeader('Content-Type'), $e);
         } catch (MappingException $e) {
             throw new BadRequestException($e->getMessage(), 400, $e);
         }
     }
     return $requestBody;
 }
Exemplo n.º 2
0
 /**
  * Send error resource to output
  * @param \Exception $e
  */
 protected function sendErrorResource(\Exception $e, $contentType = NULL)
 {
     /** @var Request $request */
     $request = $this->getHttpRequest();
     $this->resource = $this->createErrorResource($e);
     // if the $contentType is not forced and the user has requested an unacceptable content-type, default to JSON
     $accept = $request->getHeader('Accept');
     if ($contentType === NULL && (!$accept || !$this->responseFactory->isAcceptable($accept))) {
         $contentType = IResource::JSON;
     }
     try {
         $response = $this->responseFactory->create($this->resource, $contentType);
         $response = new ErrorResponse($response, $e->getCode() > 99 && $e->getCode() < 600 ? $e->getCode() : 400);
         $this->sendResponse($response);
     } catch (InvalidStateException $e) {
         $this->sendErrorResource(BadRequestException::unsupportedMediaType($e->getMessage(), $e), $contentType);
     }
 }