简体   繁体   English

RestSharp 使用默认值反序列化对 object 的 401 响应

[英]RestSharp deserializing 401 response to object with default values

I've experienced an issue while using RestSharp.我在使用 RestSharp 时遇到了问题。

I have a method in my client which is show below.我的客户端中有一个方法,如下所示。 When I send a request with a valid token I get an IEnumerable of type StravaActivityDto , and its working fine.当我使用有效令牌发送请求时,我得到一个StravaActivityDto类型的IEnumerable ,并且它工作正常。 But when I send a request and my token is not valid, instead of activities variable being null it is IEnumerable of type StravaActivityDto , but it always has only one object in it and this has a default value in every property (eg Id =0, MaxSpeed =0).但是当我发送请求并且我的令牌无效时,活动变量不是 null 而是StravaActivityDto类型的IEnumerable ,但它始终只有一个 object ,并且在每个属性中都有一个默认值(例如Id =0, MaxSpeed =0)。

目的

I have checked the behavior in Postman and if I make a request with an invalid token the status code of the response is 401.我检查了 Postman 中的行为,如果我使用无效令牌发出请求,则响应的状态代码为 401。

How can I stop RestSharp from deserializing the response if the request is invalid (which results in creating StravaActivityDto with default values).如果请求无效(这导致使用默认值创建StravaActivityDto ),我如何阻止 RestSharp 反序列化响应。
I know I can use request.Execute and then check IsSuccessful status but I would like to just get null after calling request.Get<>() .我知道我可以使用request.Execute然后检查IsSuccessful状态,但我想在调用request.Get<>()后得到 null 。

public async Task<IEnumerable<StravaActivityDto>> GetActivities(string token)
{
    var request = new RestRequest("activities", DataFormat.Json);
    request.AddHeader("authorization", "Bearer " + token);
    var activities = await RestClient.GetAsync<IEnumerable<StravaActivityDto>>(request);

    return activities;
}

This is default behavior of RestSharp, it didn't throw any errors on responses with HTTP errors, only on transport level (when server is unreachable or etc)这是 RestSharp 的默认行为,它不会在 HTTP 错误的响应中引发任何错误,仅在传输级别(当服务器无法访问等时)

So you have 2 options:所以你有2个选择:

First第一的

Add OnBeforeDeserialization on every request在每个请求上添加OnBeforeDeserialization

request.OnBeforeDeserialization = response =>
{
     if (!response.IsSuccessful)
          throw new Exception("Response is unsuccessful");
};

Second第二

Use ExecuteGetAsync instead of GetAsync method and check for errors on every response.使用ExecuteGetAsync而不是GetAsync方法并检查每个响应的错误。

var response = await client.ExecuteGetAsync<IEnumerable<StravaActivityDto>>(request);
                
if (!response.IsSuccessful)
    throw new Exception("Response is unsuccessful");

return response.Data;

also instead of using也代替使用

request.AddHeader("authorization", "Bearer " + token);

on every requests, you can use JwtAuthenticator (or your custom authenticator if you want) once in client initialization.在每个请求上,您可以在客户端初始化中使用一次 JwtAuthenticator(或您的自定义身份验证器)。

var client = new RestClient()
{
    Authenticator = new JwtAuthenticator("MyBearerToken")
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM