[英]Set Accept header on ServiceStack JsonServiceClient
I am using the ServiceStack.JsonServiceClient to attempt to make a URL request with a custom value for the Accept header, but am unable to find a way to make the call. 我正在使用ServiceStack.JsonServiceClient尝试使用接受标头的自定义值发出URL请求,但是无法找到进行调用的方法。
The service call is to retrieve the topics for a repository through the GitHub API. 该服务调用是通过GitHub API检索存储库的主题。 This is currently a preview feature, and requires additional information to be sent on the accept header as a custom media type.
当前,这是预览功能,需要其他信息作为自定义媒体类型在accept标头上发送。 ( https://developer.github.com/v3/repos )
( https://developer.github.com/v3/repos )
I'm using Service Stack and making a C# .NET call to the API endpoint, and I'm struggling to find the appropriate way to update the accept header on the request. 我正在使用Service Stack并对API端点进行C#.NET调用,而我正努力寻找适当的方法来更新请求上的accept标头。 I've tried two strageies.
我已经尝试了两个阶段。
Create an HttpWebRequest with the header and pass it to the client 创建带有标题的HttpWebRequest并将其传递给客户端
Uri uri = new Uri(requestUrl, UriKind.Absolute); HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(uri); webRequest.Accept = "application/vnd.github.mercy-preview+json"; HttpWebResponse webResponse = client.Get<HttpWebResponse>(webRequest);
The requestUrl
is a string of the form https://api.github.com/repos/{owner}/{repo}?access_token={token}
. requestUrl
是形式为https://api.github.com/repos/{owner}/{repo}?access_token={token}
的字符串。 It successfully creates both the Uri and the HttpWebRequest objects but then throws an exception on the client.Get
, claiming Invalid URI: The format of the URI could not be determined.
它成功创建了Uri和HttpWebRequest对象,但随后在
client.Get
上引发了异常,并声明Invalid URI: The format of the URI could not be determined.
Calling the API with the string URL: HttpWebResponse webResponse = client.Get<HttpWebResponse>(requestUrl);
使用字符串URL调用API:
HttpWebResponse webResponse = client.Get<HttpWebResponse>(requestUrl);
succeeds without issue (but the custom header value is not passed). 成功而没有问题(但未传递自定义标头值)。
Set the header on the client itself. 在客户端本身上设置标头。
client.AddHeaders
throws an exception because Accept
is a restricted header. client.AddHeaders
会引发异常,因为Accept
是受限制的标头。 I have been unable to find any other way to set the header directly on the client (although this would probably be the nicest solution). 我一直找不到其他方法可以直接在客户端上设置标头(尽管这可能是最好的解决方案)。
Thank you, and let me know if there is anything else that I can provide. 谢谢,让我知道我还有什么可以提供的。
You can use the RequestFilter
to modify the HttpWebRequest
before requests are sent, eg: 您可以在发送请求之前使用
RequestFilter
修改HttpWebRequest
,例如:
var client = new JsonServiceClient(baseUrl) {
RequestFilter = req => req.Accept = acceptHeader
};
Note: ServiceStack's C#/.NET Service Clients should only be used for calling ServiceStack Services. 注意:ServiceStack的C#/。NET服务客户端仅应用于调用ServiceStack服务。 For calling 3rd Party APIs you should instead use HTTP Utils , eg:
为了调用第三方API,您应该改用HTTP Utils ,例如:
var body = url.GetStringFromUrl(requestFilter: req => {
req.Accept = acceptHeader;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.