Reputation: 4516
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!
Upvotes: 12
Views: 24266
Reputation: 1045
now "Refit" accept to set multiple headers dynamic:
sample:
[Get("/users/{user}")]
Task<User> GetUser(string user, [HeaderCollection] IDictionary<string, string> headers);
var headers = new Dictionary<string, string> {{"Authorization","Bearer tokenGoesHere"}, {"X-Tenant-Id","123"}};
var user = await GetUser("octocat", headers);
Upvotes: 4
Reputation: 11
Try this :
Invoking method should be like :
var response = await GetUser(1,"Bearer <token>");
I found solution here: https://github.com/reactiveui/refit/issues/693
Upvotes: 1
Reputation: 11675
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.
Upvotes: 12