refit: Disable Refit from hiding HTTP content in case response code wasn't 200 (ApiException)

We use http code to indicate what’s wrong and send reason in json response. Currently Refit overrides this behavior with this message "Response status code does not indicate success: 400 (Bad Request)"

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

By the way, with the above GroupList method, and assuming an error response like this:

public class ErrorResponse
{
    public string Message { get;set; }
}

All you would have to do is:

try
{
    var users = api.GroupList(groupId);
    // do something with users
}
catch(ApiException ex)
{
   var statusCode = ex.StatusCode;
   var error = ex.GetContentAs<ErrorResponse>();
   // deal with error.Message or something
}

You still get the automatic mapping for successful cases, and I don’t see how it adds much work when it fails. Can you explain why this isn’t enough, and more importantly - exactly how you see it working otherwise?

That’s all great, but this means I will change all my Refit from the elegant

[Get("/group/{id}/users")]
Task<List<User>> GroupList([AliasAs("id")] int groupId);

to

Task<List<HttpResponseMessage>> GroupList([AliasAs("id")] int groupId);

Which kinda beats the purpose of Refit in the first place.

Let me share with you my implementation. I am using ASP.NET WebAPI, and I am enapsulating all my responses in a standard Json envelope:

public class JsonResponseResult<T>
    {
        //public string Version { get { return "1.2.3"; } }
        public bool Success => string.IsNullOrEmpty(ErrorMessage);
        public string ErrorMessage { get; set; }
        public T Result { get; set; }
        public JsonResponseResult(T result = default(T), string errorMessage = null)
        {
            Result = result;
            ErrorMessage = errorMessage;
        }
    }

Which the mobile has an identical one over. so my Refit is like:

[Headers ("Accept: application/json")]
public interface IMyApi
{
    [Get ("/api/entities")]
    Task<JsonResponseDto<MyDto>> Get();
}

and on mobile, I can easily check validity by doing

var response = await APIs.Get();
if (Response.Success)
{
   ///YAAAYY
}
else
{
   //BOOO
}

IMHO it should be possible to disable the Exception and offer a way to examine the response object including the Status code.