How to send custom JSON headers with requests in drf_spectacular (django)?

Viewed 427

Can I create an custom description of JSON headers in drf_spectacular without using serializers class in @extend_schema decorator?

1 Answers

You could do this with inline_serializer:

OpenApiParameter(
    name='X-Api-Version',
    type=inline_serializer(
        "JsonHeader",
        fields={
            "a": serializers.CharField,
            "b": serializers.CharField,
        }
    ),
    location=OpenApiParameter.HEADER,
),

or write a raw schema yourself:


OpenApiParameter(
    name='bbox',
    type={'type': 'object', 'properties': {'a': {}, 'b': {}}, 'required': ['a', 'b']},
    location=OpenApiParameter.HEADER,
)

You may also want to look into the style and explode parameters of OpenApiParameter for more fine-grained control.

Related