question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I tell swashbuckle to add a required header

See original GitHub issue

I have one of my API controller that requires a header to be present (this is not a security header).

From what I’ve found on the internet (http://stackoverflow.com/questions/26742521/sending-dynamic-custom-headers-in-swagger-ui-try-outs), in java you can use http://docs.swagger.io/swagger-core/apidocs/com/wordnik/swagger/annotations/ApiImplicitParam.html with a paramType=“header”

@ApiImplicitParams(
    { @ApiImplicitParam(paramType="header", name="X-CurrentLocale", dataType="string") }
)

How do I do the same in Swashbuckle ?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

24reactions
domaindrivendevcommented, Sep 25, 2015

In addition, if you’d like to list the header as a parameter with each operation description, you can just wire it up via an IOperationFilter (see readme). Here’s some sample code to get you started:

public class AddRequiredHeaderParameter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        if (operation.parameters == null)
            operation.parameters = new List<Parameter>();

        operation.parameters.Add(new Parameter
            {
                name = "Foo-Header",
                @in = "header",
                type = "string",
                required = true
            });
    }
} 
4reactions
tojoirinahcommented, Dec 18, 2019

In addition, if you’d like to list the header as a parameter with each operation description, you can just wire it up via an IOperationFilter (see readme). Here’s some sample code to get you started:

public class AddRequiredHeaderParameter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        if (operation.parameters == null)
            operation.parameters = new List<Parameter>();

        operation.parameters.Add(new Parameter
            {
                name = "Foo-Header",
                @in = "header",
                type = "string",
                required = true
            });
    }
} 

This works great but is there a way to do this and then exclude it from one or two places?

It works only for post method, how about get ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Web Api How to add a Header parameter for all API in ...
If swagger is used in ASP.Net MVC5, and required to add headers to get input from swagger UI. Give reference of this class...
Read more >
Adding a Required HTTP Header to Your Swagger UI With ...
Let's first look at a simple swagger setup as our baseline before we add everything for our HTTP Header Field. ... config.SwaggerDoc( "v1"...
Read more >
Swagger UI - Add required header
Let's see how we can get this done through SwashBuckle. First I need to create a custom IOperationFilter that will add the header: ......
Read more >
Add a Header parameter to .NET Core API in Swagger
Add a Custom header parameter to .NET Core API in Swagger –OpenAPI · Step1 – Create ASP.NET Core API · Step2: Install the...
Read more >
Add an authorization header to your swagger-ui with ...
First, you need to tell Swashbuckle what security your API has: ... to do is tell Swashbuckle which of our actions require Authorization....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found