Solved the problem.
Now I just need to figure out how to access the fields of my response Body
{"id":21,"userName":"fabiopires22","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjIxIiwibmJmIjoxNjA3NzgzODE5LCJleHAiOjE2MDgzODg2MTksImlhdCI6MTYwNzc4MzgxOX0.bfl3_6YVgcuFPbgZnD6rMjcNFaYdHdVjvArKTyKc__Q"}
public static async Task<User> Login(string username, string password)
{
string responseBody;
UserLogin userLogin = new UserLogin
{
Username = username,
Password = password
};
User user = null;
HttpResponseMessage response = await client.PostAsJsonAsync(
"User/Login", userLogin);
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
Debug.WriteLine("RESPONSE BODY: " + responseBody);
//User user = JsonConvert.DeserializeObject<User>(responseBody);
//Debug.WriteLine("A RESPOSTA FOI :" + user.GetUsername());
return user;
}
Just deserialize the JSON, the code you commented out should do it
If my credentials are wrong I want to return a null user. Something like this
public static async Task<User> Login(string username, string password)
{
string responseBody;
UserLogin userLogin = new UserLogin
{
Username = username,
Password = password
};
User user = null;
HttpResponseMessage response = await client.PostAsJsonAsync(
"User/Login", userLogin);
response.EnsureSuccessStatusCode();
Debug.WriteLine("RESPONSE : " + response);
//if (response.StatusCode == 200)
// {
responseBody = await response.Content.ReadAsStringAsync();
user = JsonConvert.DeserializeObject<User>(responseBody);
// }
Debug.WriteLine("A RESPOSTA FOI :" + user.UserName);
return user;
}
Should I delete
response.EnsureSuccessStatusCode();
When ever I do an API like this I always have it return a Status object, something like:
{
āidā: 123,
āuserā: āusernameā,
āstatusā: {
ācodeā: 0,
āmessageā: āBad user credentailsā
}
}
Can I replace UserLogin by a generic object?
public static class Communication
{
private static readonly HttpClient client = new HttpClient();
public static async Task<User> Login(string username, string password)
{
User user = null;
try
{
string responseBody;
UserLogin userLogin = new UserLogin
{
Username = username,
Password = password
};
HttpResponseMessage response = await client.PostAsJsonAsync("User/Login", userLogin);
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
user = JsonConvert.DeserializeObject<User>(responseBody);
}
catch (Exception ex)
{
Debug.WriteLine("\tERROR {0}", ex.Message);
}
return user;
}
public static void Initialize()
{
client.BaseAddress = new Uri("http://localhost:5000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
}
public class UserLogin{
public string Username { get; set; }
public string Password { get; set; }
}
persn
December 12, 2020, 5:08pm
26
Not sure what you mean by generic object here, but C# has a dynamic
return type you can try https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic
I will try to explain in a JavaScript style. For example in JavaScript I can create a JsonObject. I intend to do the same here so I can delete my UserLogin class
persn
December 12, 2020, 5:37pm
28
Basically you need a type that can accept a JSON no matter what data it has? Thatās what the dynamic
type I linked to is used for
So I have to do something like this?
dynamic userLogin = new object
{
Username = username,
Password = password
};
persn
December 12, 2020, 6:04pm
30
No you canāt insert random properties into the object class type. Here are some examples with JSON and dynamic https://weblog.west-wind.com/posts/2012/aug/30/using-jsonnet-for-dynamic-json-parsing
I“ve done this
var jsonObject = new JObject();
dynamic userLogin = jsonObject;
userLogin.Username = username;
userLogin.Password = password;
but now I am getting this error