I am trying to consume a post API like this:
var client = new RestClient("https://linkdelAPI/create_or_update.json");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic asmhskashaklsajskajsajsasklsasas==");
//ACA EL PROBLEMA, NO SE COMO ENVIAR EL USER:{}, LOS CAMPOS NAME Y EMAIL VAN DENTRO DE USER
request.AddParameter("name", "Nombre de Prueba", ParameterType.RequestBody);
request.AddParameter("email", "[email protected]", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
From the postman I send this type of data, but in RestSharp C# I don't know how to send it. Please, if anyone knows the data, tell me how.
Thanks for the help. Take the example they sent me and do it like this with RestSharp.
var client = new RestClient("https://linkdelapi");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
DatosUser u = new DatosUser();
u.user = new User
{
name = name,
email = email
};
var j = JsonConvert.SerializeObject(u);
request.AddHeader("Authorization", "Basic token");
request.AddParameter("application/json", j, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
First you create the service class and you need a class to create your json. Then you call the service to pass the necessary information, it parses your object to json, then creates it
StringContent
with its parameters and sends everything throughPost
. To verify that everything went well, you have theif
with said verificationstatus
of the response.// From your form or program.cs or wherever you want to call it, you can do it like this.