3
  • I configured Swagger UI in my Web API MVC5. For testing API's, swagger works fine until I add some header parameters.
  • API's are created in such way that username & password fields are in headers and all other data is taken in the body.
  • Headers are added by IOperationFilter as suggested in Web Api How to add a Header parameter for all API in Swagger and OperationFilter is added in SwaggerConfig.cs as: c.OperationFilter<AddHeaderParameters>();
    public class AddHeaderParameters : 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 = "Account Username",
                @in = "header",
                type = "string",
                required = true,
            });
            operation.parameters.Add(new Parameter
            {
                name = "Account Password",
                @in = "header",
                type = "string",
                required = true,
            });
        }
    }
    

Swagger UI with Headers enter image description here

Required help that which thing I have missed or something done not correct.

Sh.Imran
  • 1,035
  • 7
  • 13

1 Answers1

1

I got the solution of issue facing as mentioned above in the question. Sharing here might be helpful for others.

As I created the class AddHeaderParameters for getting headers in swagger UI, I had given the header name self defined (that was actual cause of issue). The header name supposed to match as actual header name.

In my case, e.g. Header name was AccountUserName, which I used in swagger header with giving space as Account Username.

Correct way

operation.parameters.Add(new Parameter
{
    name = "AccountUserName",
    @in = "header",
    type = "string",
    required = true,
});
Sh.Imran
  • 1,035
  • 7
  • 13