Doubt with Webapi data insertion

Asked

Viewed 132 times

0

I’m using the Postman: inserir a descrição da imagem aqui Error: "Undefined object reference for an object instance."

I have the Controller Code:

//http://localhost:1608/api/ApiGuiaCidade/cadastrar/cliente
        //"Referência de objeto não definida para uma instância de um objeto."
        [HttpPost]
        [Route("cadastrar/cliente/")]
        public HttpResponseMessage PostCadastro(Cliente cliente)
        {
            try
            {
                var tCliente = new ClienteAplicacao();
                tCliente.Inseri(cliente);
                return Request.CreateResponse(HttpStatusCode.OK, "Cadastro do cliente" + cliente.Nome + "realizado.");
            }
            catch (Exception ex )
            {

                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }

Have of the application:

        public void Inseri(Cliente cliente)
        {
            var strQuery = "";
            strQuery += "INSERT INTO CLIENTES (NOME, DATA_NASCIMENTO,EMAIL, SENHA)";
            strQuery += string.Format(" VALUES ('{0}','{1}','{2}','{3}' )", cliente.Nome, cliente.DataNascimento, cliente.Email, cliente.senha);

            using (contexto = new Contexto())
            {
                contexto.ExecutaComando(strQuery);
            }

        }
  • How are you firing the payload by Postman? You can take a picture of the screen?

  • 1

    Why do you put syntax highlighting as html?

  • I can do the editing by adding a photo, but the error now is: Could not get any Response This seems to be like an error Connecting to http://localhost:1608/api/Apiguiacity/register/client. The Response status was 0. Check out the W3C Xmlhttprequest Level 2 spec for more Details about when this Happens.

1 answer

3

See, the error says (my emphasis)

The request Entity’s media type 'text/Plain' is not supported for this Resource.

When it’s time to do the POST for Postman you need to change the content type for application/json

  • I made the change, now I have the error: Could not get any Response This seems to be like an error Connecting to http://localhost:1608/api/Apiguiacity/register/client. The Response status was 0. Check out the W3C Xmlhttprequest Level 2 spec for more Details about when this Happens.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.