A 400 means the parameters are not correct. Typically there is no response body, but if there is, it’s an error message.
Need help to handle StatusCode:200 and StatusCode:400

Jerry Lipan
916
Reputation points
This is Web API result. return with Json data and return without Json data
with Json data
without Json data
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
This give an error
Please help. I'm stuck