I am creating a method that uses optional headers and if you make a request directly to the API works, but the swagger creates them as required. I am using NestJS as a framework.
This is the code for the request on the controller:
createShoppingCart(
@Headers('authToken') authToken?: string ,
@Headers('sessionToken') sessionToken?: string,
@Headers('segmentToken') segmentToken?: string,
): Promise<GetCartInformationResponseDto> {
return this.shoppingCartsService.createShoppingCart(
authToken,
sessionToken,
segmentToken,
);
}
And this is how the swagger displays the Headers.

If I add the "or undefined" option it does not even show it into the swagger:
@Headers('authToken') authToken?: string | undefined,
Does anyone knows how to solve this?
to make headers optional you can use @ApiHeader() decorator in your controller's route.
@Get()
@ApiHeader({
name: 'x-skip-cache', // your header name
required: false, // Set required to false to make it optional
description: 'Optional header to skip cache',
})
async getLocationsData(
@Headers('x-skip-cache') skipCache: string = 'false',
){}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With