0
I’m studying Webapi and using Postman to test it. Doing some tests I realized that nothing comes when I send json using Postman to Webapi. I researched a lot about POST using Webap, but I don’t know why it’s not working...
Follow the Controller code:
using Microsoft.AspNet.Mvc;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Model;
namespace WebApi.Controllers
{
[Route("api/[controller]")]
public class ClienteController : ApiController
{
private IList<Cliente> novosClientes = new List<Cliente>();
private Cliente[] Clientes = new Cliente[]
{
new Cliente { ID = 1, Nome = "Joel Jordison", Email = "[email protected]", Ativo = true },
new Cliente { ID = 2, Nome = "Bill Gates", Email = "[email protected]", Ativo = true },
new Cliente { ID = 3, Nome = "Aleister Crowley", Email = "[email protected]", Ativo = false }
};
// GET: api/cliente
[HttpGet]
public Cliente[] Get()
{
return Clientes;
}
// POST api/cliente
[HttpPost]
public HttpResponseMessage Post(Cliente value)
{
Debug.WriteLine("Começo");
Debug.WriteLine("-------------Value-----------------");
Debug.WriteLine(value.ID);
Debug.WriteLine(value.Nome);
Debug.WriteLine(value.Email);
Debug.WriteLine(value.Ativo);
Debug.WriteLine("-------------Fim Value-------------");
if (value != null)
{
Debug.WriteLine("Não nulo");
novosClientes.Add(value);
Clientes = novosClientes.ToArray();
return new HttpResponseMessage(HttpStatusCode.OK);
}
Debug.WriteLine("Fim");
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
}
}
I’m trying to send the json by selecting POST, then I Body and putting the json in Postman like this:
{
"ID": 10,
"Nome": "Joana",
"Email": "[email protected]",
"Ativo": true
}
As an answer, I get this:
{
"Version": {
"Major": 1,
"Minor": 1,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"Content": null,
"StatusCode": 200,
"ReasonPhrase": "OK",
"Headers": [],
"RequestMessage": null,
"IsSuccessStatusCode": true
}
I still can’t do a POST. When I try to do the POST I get this
XMLHttpRequest cannot load http://localhost:50143/api/Cliente. Response for preflight has invalid HTTP status code 405
– Iago Frota