简体   繁体   English

在ServiceStack JsonServiceClient上设置Accept标头

[英]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. 我已经尝试了两个阶段。

  1. 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). 成功而没有问题(但未传递自定义标头值)。

  2. 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.

相关问题 ServiceStack JsonServiceClient 响应头 - ServiceStack JsonServiceClient Response Header ServiceStack JsonServiceClient 请求不一致 - ServiceStack JsonServiceClient Requests not consistent ServiceStack.JsonServiceClient.HttpLog 未填充 - ServiceStack.JsonServiceClient.HttpLog is not populating 反序列化时,ServiceStack JsonServiceClient静默失败 - ServiceStack JsonServiceClient Silently Failing When Deserializing ServiceStack JsonServiceClient,强制为localhost线路上的流量? - ServiceStack JsonServiceClient, force traffic on the wire for localhost? ServiceStack JsonServiceClient使用路由属性发送会引发未找到异常 - ServiceStack JsonServiceClient Send using route attributes throws exception not found 类型&#39;ServiceStack.JsonServiceClient&#39;中的方法&#39;Get&#39;…没有实现 - Method 'Get' in type 'ServiceStack.JsonServiceClient' … does not have an implementation 提取数据时,ServiceStack MsgPackServiceClient失败,但JsonServiceClient起作用 - ServiceStack MsgPackServiceClient fails when fetching data but JsonServiceClient works 什么是在Servicestack JsonServiceClient Get方法上实现重试的最佳解决方案? - Whats the best solution to implement retry on Servicestack JsonServiceClient Get method? ServiceStack:如何从JsonServiceClient的Get方法获取StatusCode - ServiceStack :How to get StatusCode from JsonServiceClient Get method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM