[英]ServiceStack JsonServiceClient Response Header
I'm new to ServiceStack so please forgive my ignorance...我是 ServiceStack 的新手,所以请原谅我的无知...
How can I the JsonServiceClient
to give my my DataContract
response object and also allow me to get the response header with only a single request?我怎样才能让
JsonServiceClient
提供我的DataContract
响应对象并允许我仅通过一个请求获取响应标头?
I've figured out how to return a populated DataContract Response object like this:我已经想出了如何像这样返回一个填充的 DataContract Response 对象:
var field1 = "aaa";
var field2 = "bbb";
var client = new JsonServiceClient("https://api.server.com/api");
Response1 response1 = client.Post(new Request1() { Field1= field1, Field2= field2});
I've also figured out how to get a response header item from the HttpWebResponse
overload like this (see below) which requires the slight modification of commenting out the request data contract interface as shown here: class Request1 //: IReturn<Response1>
:我还想出了如何从这样的
HttpWebResponse
重载中获取响应标头项(见下文),这需要稍微修改注释掉请求数据协定接口,如下所示: class Request1 //: IReturn<Response1>
:
var field1 = "aaa";
var field2 = "bbb";
var client = new JsonServiceClient("https://api.server.com/api");
HttpWebResponse response1 = client.Post(new Request1() { Field1= field1, Field2= field2});
var value = response1.Headers.GetValues("Item")[0];
But again, how can I do both at once?但同样,我怎么能同时做到这两点呢?
I did figure out how to do this with the .PostJsonToUrl()
extension method but my question is how can I do this with the JsonServiceClient
.我确实想出了如何使用
.PostJsonToUrl()
扩展方法来做到这一点,但我的问题是如何使用JsonServiceClient
做到这JsonServiceClient
。 If anyone is curious here's how I did it with the .PostJsonToUrl()
extension method:如果有人好奇这里是我如何使用
.PostJsonToUrl()
扩展方法.PostJsonToUrl()
:
var url = "https://api.server.com/api/method";
string value = null;
Response1 response1 = url.PostJsonToUrl(new Request1() { Field1= field1, Field2= field2},
null, httpResponse =>
{
value = httpResponse.Headers.GetValues("Item")[0];
}).FromJson<Request1>();
Here are my DataContract Classes这是我的 DataContract 类
[Route("/method")]
[DataContract]
class Request1 : IReturn<Response1>
{
[DataMember(Name = "field1")]
public string Field1 { get; set; }
[DataMember(Name = "field2")]
public string Field2 { get; set; }
}
[DataContract]
class Response1
{
[DataMember(Name = "field1")]
public long Field1 { get; set; }
[DataMember(Name = "field2")]
public string Field2 { get; set; }
[DataMember(Name = "field3")]
public string Field3 { get; set; }
}
All ServiceStack's .NET Clients include both a ResponseFilter
and static GlobalResponseFilter
property you can use to inspect the HTTP Response before the response is deserialized, eg:所有ServiceStack 的 .NET 客户端都包含
ResponseFilter
和静态GlobalResponseFilter
属性,您可以使用它们在反序列化响应之前检查 HTTP 响应,例如:
string myHeader = null;
var client = new JsonServiceClient(baseUrl) {
ResponseFilter = res => myHeader = res.Headers["X-MyHeader"],
};
var response = client.Post(request);
Where response
will contain the deserialized Response body and myHeader
will be populated with the value of X-MyHeader
HTTP Header.其中
response
将包含反序列化的 Response 正文,而myHeader
将填充X-MyHeader
HTTP Header 的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.