Need help to handle StatusCode:200 and StatusCode:400

Jerry Lipan 916 Reputation points
2021-06-27T05:27:41.45+00:00

This is Web API result. return with Json data and return without Json data

with Json data
109613-200.png

without Json data
109614-400.png

This is the Models

using System.Collections.Generic;  
using System.Text;  
  
namespace App1Client.Model.Login  
{  
    public class User  
    {  
        public string userId { get; set; }  
    }  
  
    public class LoginApiResponseModel  
    {  
        public string authenticationToken { get; set; }  
        public User user { get; set; }  
    }  
}  
  

This is my code so far,

public async Task<LoginApiResponseModel> AuthenticateUserAsync(string phonenumber, string password)  
        {  
            try  
            {  
                LoginApiRequestModel loginRequestModel = new LoginApiRequestModel()  
                {  
                    Email = phonenumber,  
                    Password = password  
  
                };  
                var content = new StringContent(JsonConvert.SerializeObject(loginRequestModel), Encoding.UTF8, "application/json");                  
                var response = await client.PostAsync("api/token", content);  
                  
                if (response.IsSuccessStatusCode)  
                {  
                    using (var stream = await response.Content.ReadAsStreamAsync())  
                    using (var reader = new StreamReader(stream))  
                    using (var json = new JsonTextReader(reader))  
                    {  
                        var jsoncontent = _serializer.Deserialize<LoginApiResponseModel>(json);  
                        Preferences.Set("authToken", jsoncontent.authenticationToken);  
                        return jsoncontent;  
                    }  
                }  
                else  
                {  
                    using (var stream = await response.Content.ReadAsStreamAsync())  
                    using (var reader = new StreamReader(stream))  
                    using (var json = new JsonTextReader(reader))  
                    {  
                        var jsoncontent = _serializer.Deserialize<LoginApiResponseModel>(json);  
                        Preferences.Set("authToken", jsoncontent.authenticationToken);  
                        return jsoncontent;  
                    }  
                }  
            }  
            catch (Exception ex)  
            {  
                return null;  
            }  
        }  

If StatusCode=400, I don't know how to return Json with null
109588-500.png

This give an error
109604-600.png

Please help. I'm stuck

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,124 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,236 questions
{count} votes

Accepted answer
  1. Bruce Barker 801 Reputation points
    2021-06-27T19:18:22.88+00:00

    A 400 means the parameters are not correct. Typically there is no response body, but if there is, it’s an error message.


0 additional answers

Sort by: Most helpful