If you’re building ASP.NET Core Web APIs, then I hope you’ve heard of Swashbuckle – the tool to generate the Swagger UI automatically for all of your controllers to make manual testing your endpoints visual and simple.
Out of the box, the documentation helps you set up your UI, handle different ways to authenticate (which we will touch on in a later post), and have it all hooked up to your controllers. However, this only handles the most common cases of required requests with query string parameters and HTTP Body content.
In this post, we’ll look at a quick and easy way to also add fields for your custom HTTP Request Headers so you can fill them out while testing without having to do some funky stuff in the console.
Basic Swagger Setup
First thing’s first, install that puppy:
Package Manager : Install-Package Swashbuckle.AspNetCore CLI : dotnet add package Swashbuckle.AspNetCore
Let’s first look at a simple swagger setup as our baseline before we add everything for our HTTP Header Field.
Startup.cs
//... public void ConfigureServices(IServiceCollection services) { // ... services.AddSwaggerGen(config => { config.SwaggerDoc("v1", new Info { Title = "My API", Version = "V1" }); }); // ... } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // ... app.UseMvc(); app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "My API v1"); }); // ... } //...
This setup gives us all we need for our basic UI and wireup to our controllers!
Running this gives us our basic swagger at /swagger
:
Adding Custom Headers
What we have to do now is add an OperationFilter
to our swagger generation. These OperationFilters
can do a whole lot and enable us to customize the swagger document created which is what drives the fields and info on the UI.
Let’s create a MyHeaderFilter
and then add it to the AddSwaggerGen
call.
MyHeaderFilter.cs
/// <summary> /// Operation filter to add the requirement of the custom header /// </summary> public class MyHeaderFilter : IOperationFilter { public void Apply(Operation operation, OperationFilterContext context) { if (operation.Parameters == null) operation.Parameters = new List<IParameter>(); operation.Parameters.Add(new NonBodyParameter { Name = "MY-HEADER", In = "header", Type = "string", Required = true // set to false if this is optional }); } }
And now we need to update our Startup
class:
Startup.cs
//... public void ConfigureServices(IServiceCollection services) { // ... services.AddSwaggerGen(config => { config.SwaggerDoc("v1", new Info { Title = "My API", Version = "V1" }); config.OperationFilter<MyHeaderFilter>(); }); // ... }
Now when we run and navigate to our /swagger
url, we can see:
When we fill out that field, we can now pull the value off the request header in our controller!
ContentController.cs
[Route("api/[controller]")] public class ContentController : Controller { public void Search([FromBody]InputModel model) { Console.WriteLine(Request.Headers["MY-HEADER"]); } }
In further posts, we’ll talk about adding Bearer Authentication, XML Comments, and More 🙂
If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile developer tips and tricks!
Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.
Very helpful. Thank you!
LikeLike
Thanks a lot, this I helpful
LikeLike
Helpful article. Thanks for sharing.
What if this header is only needed on only one controller or one operation?
LikeLike
The name of the controller and method are available in the “operation”-object in your filter, so you can check those (the OperationId and Tags -fields)
LikeLike
Exactly what I needed. Thanks for the help.
LikeLike
Thanks for sharing.
LikeLike
Thank you very much, very helpful 🙂
LikeLike
Very helpful, thank you 🙂
LikeLike
Thank you!
LikeLike