Adding Default Headers to Swagger (OpenAPI)

Recently, we had a requirement to pass a mandatory/ default header to all our HTTP POST requests. Our application is built on ASP.NET Core 3.1 and uses Swagger to describe and expose our Web APIs to the consumer.

To ensure that each POST request includes the required header, I intended to add the header information to the Swagger (now OpenAPI) Definition. Here’s how I was able to achieve this.

To use Swagger/ Open API in your .NET Core 3+ application, you need to use Swashbuckle.AspNetCore version 5.0 in your project. At the time of writing, this NuGet package is still in preview.

dotnet add package Swashbuckle.AspNetCore

A typical ASP.NET Core 3.1 Startup project with a Swagger definition looks as below:

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My Awesome Application", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("v1/swagger.json", "My Awesome Application");
});
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
view raw Startup.cs hosted with ❤ by GitHub

To add the default header to each POST request, implement IOperationFilter as below:

using System;
using System.Net.Http;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace MyAwesomeApplication
{
public class DefaultHeaderFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (string.Equals(context.ApiDescription.HttpMethod, HttpMethod.Post.Method, StringComparison.InvariantCultureIgnoreCase))
{
operation.Parameters.Add(new OpenApiParameter
{
Name = "my-default-header",
In = ParameterLocation.Header,
Required = false,
Example = new OpenApiString("my-default-header-value")
});
}
}
}
}

Now, Update AddSwaggerGenCommand in Startup → ConfigureServices method to include the new DefaultHeaderFilter

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My Awesome Application", Version = "v1" });
c.OperationFilter<DefaultHeaderFilter>();
});
}
view raw Startup.cs hosted with ❤ by GitHub

That’s it! Your Swagger UI will now have the default header for each POST request with this change.

Swagger with Default Header

Posted

in

, ,

by

Comments

One response to “Adding Default Headers to Swagger (OpenAPI)”

  1. ProgramWithPaul Avatar
    ProgramWithPaul

    Worked like a charm, Ankit, thank-you!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

A WordPress.com Website.

%d bloggers like this: