0

So I decided to use this method since it's the simplest to add custom headers for my api calls, example:

[HttpPost]
[Route("something")]
public async Task<somethingObject> DoSomething([Microsoft.AspNetCore.Mvc.FromHeader(Name = "deviceGuid")] string deviceGuid)
{
    var somethingObject= await doSomethingWithDevice(deviceGuid)
    return somethingObject;
}

The expected outcome from this is a field in Swagger where I can input the deviceGuid and Swagger should consider it as a header.

Issue at hand is that Swagger is considering it a query and not a header:

image

Any clue how I can solve this?

1 Answer 1

1

I dont think SwashBuckle (im guessing this is the swagger implementation you're using), knows about FromHeader.
What you could do, is this;
Make an OperationFilter that changes the ParameterType to Header - something like the following; - note I found this in this gist.

public class FromHeaderAttributeOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        foreach (var httpParameterDescriptor in apiDescription.ActionDescriptor.GetParameters().Where(e => e.GetCustomAttributes<FromHeaderAttribute>().Any()))
        {
            var parameter = operation.parameters.Single(p => p.name == httpParameterDescriptor.ParameterName);
            parameter.name = httpParameterDescriptor.GetCustomAttributes<FromHeaderAttribute>().Single().HeaderName;
            parameter.@in = "header";
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I ended up using if (apiDescription.RelativePath.Contains("/registration")) { add custom headers }
You can do that aswell - works perfect when you only got the one route to fix :)
Yeah I had 2-3 routes so I ended up using 3 ifs to check the routes, more of a quick nice fix than a permanent one though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.