Refit - dynamic and static title
I am using Refit and would like to set both dynamic and static title. For this one particular call, I need to set the application content type / json (for others, I don't), but I also need to pass in a dynamic bearer token.
I am getting a 500 error, it looks like one header is erasing the other.
Is this valid and will it be passed for both content type and authorization: media?
[Headers("Content-Type: application/json")]
[Post("api/myendpoint")]
Task<bool> GetUser([Body]int id, [Header("Authorization")] string bearerToken);
Thank!
source to share
Sending dynamic and static headers simultaneously 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 find the headers are not set correctly, please raise the issue on Github and ideally provide a small repo project we can look at.
source to share
Try it:
The calling method should be like this:
var response = await GetUser(1,"Bearer <token>");
I found a solution here: https://github.com/reactiveui/refit/issues/693
source to share