Reputation: 220
I consumed a get api by creating a base service like that:
public class BaseService<T> where T : class
{
private string baseUrl = "http://192.168.43.137:45455/api";
public IEnumerable<T> GetRequest(string uri)
{
baseUrl += uri;
var client = new RestClient(baseUrl);
var request = new RestRequest(Method.GET);
var response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string json = response.Content;
return JsonConvert.DeserializeObject<IEnumerable<T>>(json);
}
return default;
}
and a userService like this:
public class UserServices {
public IEnumerable<User> GetUsers()
{
var requestService = new BaseService<User>();
return requestService.GetRequest("/Users");
}
It worked fine for the get but I don't know how to consume the post api like this
Upvotes: 0
Views: 439
Reputation: 1934
You can check the microsoft document "Consume a RESTful web service":
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest
It shows you how to deal with the different kinds of requests, and there is a sample link in the bottom which you can refer to.
Upvotes: 1