我正在使用Refit,并且想同时设置动态和静态标头。对于这个特定的调用,我需要设置一个application / json的内容类型(对于其他人,则不需要),但是我还需要传递一个动态的承载令牌。
我收到500错误,似乎一个标头正在擦除另一个标头。
这是否有效,是否会同时传递内容类型和授权:bearer?
[Headers("Content-Type: application/json")]
[Post("api/myendpoint")]
Task<bool> GetUser([Body]int id, [Header("Authorization")] string bearerToken);
Run Code Online (Sandbox Code Playgroud)
谢谢!
Refit支持同时发送动态和静态标头。这是一个工作示例:
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"));
Run Code Online (Sandbox Code Playgroud)
将以下内容写入控制台:
"{
"headers": {
"Connection": "close",
"Host": "httpbin.org",
"X-Bar": "bar",
"X-Foo": "123"
}
}"
Run Code Online (Sandbox Code Playgroud)
如果您发现标头设置不正确,请在Github上提出问题,最好提供一个小型的propro项目供我们参考。