python-3.xdjangodjango-rest-frameworkdrf-spectacular

How to Set Responses Media Type to application/octet-stream in drf-spectacular?


I am currently using drf-spectacular for schema generation in my Django project.
I need to set the media type of the responses to application/octet-stream in the @extend_schema decorator, but I am not sure how to do this.
Could anyone provide guidance or examples on how to achieve this? Thank you in advance!

paths:
  /file-download:
    get:
      summary: Download a file
      description: This API endpoint allows users to download a binary file.
      responses:
        '200':
          description: File downloaded successfully
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
                description: Binary file

Looks like this


Solution

  • When you use extend_schema decorator. Responses can be detailed further by providing a dictionary instead. This could be for example {201: YourResponseSerializer, ...} or {(200, 'application/pdf'): OpenApiTypes.BINARY, ...}. Link to documentation https://drf-spectacular.readthedocs.io/en/latest/customization.html