I'm studying WebAPI and using Postman to test. By doing some tests I realized that nothing comes when I send json using Postman for WebAPI. I searched a lot about POST using WebAP, but I do not 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 json by selecting POST, then I Body and putting json in Postman like this:
{
"ID": 10,
"Nome": "Joana",
"Email": "[email protected]",
"Ativo": true
}
In response, 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
}