Refit - Dynamic AND Static Header

NullHypothesis picture NullHypothesis · Apr 18, 2017 · Viewed 8.3k times · Source

I am using Refit and would like to set both a dynamic AND a static header. For this one specific call I need to set a content-type of application/json (for others, I do not), but I also need to pass a dynamic bearer token.

I'm getting a 500 error it almost seems like the one header is erasing the other.

Is this valid and will it pass both content-type AND authorization: bearer ?

[Headers("Content-Type: application/json")]
[Post("api/myendpoint")]
Task<bool> GetUser([Body]int id, [Header("Authorization")] string bearerToken);

Thanks!

Answer

Bennor McCarthy picture Bennor McCarthy · Sep 12, 2017

Sending dynamic and static headers at the same time is supported by Refit. Here's a working example:

public interface IHttpBinApi
{
    [Headers("X-Foo: 123")]
    [Get("/headers")]
    Task<dynamic> GetHeaders([Header("X-Bar")] string bar);
}

// And in the consumer
Console.WriteLine(await api.GetHeaders("bar"));

Which writes the following to the console:

"{
  "headers": {
    "Connection": "close",
    "Host": "httpbin.org",
    "X-Bar": "bar",
    "X-Foo": "123"
  }
}"

If you are finding that the headers are not being set correctly, please raise an issue on Github and ideally provide a small repro project that we can look at.