Reputation: 122
I've been using the new ServiceStack API like this:
[Route("/me/login/")]
public class LoginRequest : IReturn<LoginResponse>
{
public string password { get; internal set; }
public string username { get; internal set; }
}
public class LoginResponse
{
public string token { get; set; }
}
...
client.PostAsync(new LoginRequest { username = email, password = password });
However, the server is a Django REST Api. So when I type an incorrent password I get the following response:
{
"non_field_errors": [
"Unable to login with the given credentials."
]
}
How can I retrieve the information in the non_field_errors
property leveraging the new ServiceStack API? (What should I override?)
Upvotes: 2
Views: 114
Reputation: 143284
ServiceStack Service Clients are only meant to be used for ServiceStack Services, I'd recommend using ServiceStack's HTTP Utils for calling 3rd party services.
The Response DTO to match that JSON would look like:
public class LoginResponse
{
public string token { get; set; }
public string[] non_field_errors { get; set; }
}
If the response returns a non 200 HTTP Error than you'll need to read the JSON from the error body response instead, e.g:
try
{
var response = baseUrl.CombineWith("me/login")
.PostJsonToUrl(new LoginRequest { ... })
.FromJson<LoginResponse>();
}
catch (Exception ex)
{
HttpStatusCode? errorStatus = ex.GetStatus();
string errorBody = ex.GetResponseBody();
var errorDto = errorBody.FromJson<LoginResponse>();
}
Upvotes: 2