This is Web API result return with Json data and return 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
If StatusCode=400, I don't know how to return Json with null
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;
}
}
Please help. I'm stuck