I need some help with running a POST. Well, when I try to call my API ( link ) to execute a Post, I can not pass the JSON for her.
The following is the API used:
// POST: api/Teste
public void Post([FromBody]string json)
{
ClassRetorno cr = new ClassRetorno();
if (string.IsNullOrEmpty(json))
{
cr = JsonConvert.DeserializeObject<ClassRetorno>("{\"Id\":1,\"Nome\":\"From Api\",\"Email\":\"[email protected]\"}");
}
else
{
cr = JsonConvert.DeserializeObject<ClassRetorno>(json);
}
mysql = new ClassMySQL();
if (mysql.conectar())
{
mysql.insert("teste", "id, nome, email", cr.Id + ",'" + cr.Nome + "','" + cr.Email + "'");
}
}
In short: If the string Json is null or empty, it will save to the bank (Id: 1, Name: From Api, Email: [email protected])
If not, it will save according to the received parameter.
This is the method that runs Post:
public async Task Posting(ClassRetorno item)
{
using (var client = new HttpClient())
{
var uri = new Uri("http://www.afectus.com.br/api/teste");
var json = JsonConvert.SerializeObject(item);
var content = new StringContent(json, Encoding.UTF8, "text/json");
HttpResponseMessage response = new HttpResponseMessage();
response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
await DisplayAlert("Alert", "Post executado!", "Ok");
}
else
{
await DisplayAlert("Alert", "Post não executado!", "Ok");
}
}
}
If anyone knows the possible cause of this problem, or at least, can give some guidance, I will be grateful.
Obs : I used the Postman tool to test the API and it still did not work.