Skip to content

Latest commit

 

History

History
41 lines (33 loc) · 1.19 KB

ACCEPTS.MD

File metadata and controls

41 lines (33 loc) · 1.19 KB

Accepts

Specify the accepted media type for the route. Only requests with the corresponding Content-Type will match the attributed route which can either be applied to:

  • Class - applies this accepted media type to all routes contained within, but can be overridden at route level
  • Method - applies this accepted media type to the route associated to the method

Definition

#[Accepts(string|MediaType $mediaType)]

Targets

  • Attribute::TARGET_CLASS
  • Attribute::TARGET_METHOD

Example

Code:

    #[Method(HttpMethod::GET)]
    #[Path('/example')]
    #[Accepts(MediaType::APPLICATION_JSON)]
    public function acceptsExample(): ResponseBuilder
    {
        return (new ResponseBuilder())
            ->setData('accepted')
            ->setContentType(MediaType::APPLICATION_JSON)
            ->setHttpStatusCode(HttpStatusCode::HTTP_OK);
    }

cURL:

curl --request POST \ 
    --location 'http://localhost/example' \
    --header 'Content-Type: application/json'