Api with Item Array does not receive the posted data

Asked

Viewed 438 times

0

I have an Api that receives the posted data as it shows the image, it turns out the data is coming empty, I appreciate the help.

inserir a descrição da imagem aqui

   //http://localhost:49764/api/unidade/carrinho/consultaUnidadeAtendimento
        [HttpPost]
        [Route("unidade/carrinho/consultaUnidadeAtendimento")]
        public HttpResponseMessage ConsultaUnidadeAtendimento(ConsultaUnidadeAtendimentoModel consultaAtendimento)
        {

            try
            {
                string numeroCarrinho = consultaAtendimento.NumeroCarrinho.ToString();
                string cep = consultaAtendimento.Cep;
                bool retiraLocal = consultaAtendimento.RetiraNoLocal;

               var tTabela = "";
               var listar = "";
               return Request.CreateResponse(HttpStatusCode.OK, new { usuario = listar.ToArray() });
            }
            catch (Exception ex)
            {

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

        }



        public class ConsultaUnidadeAtendimentoModel
        {
            [JsonProperty("numeroCarrinho")]
            public long NumeroCarrinho { get; set; }

            [JsonProperty("itens")]
            public dynamic Itens { get; set; }

            [JsonProperty("cep")]
            public string Cep { get; set; }

            [JsonProperty("retiraNoLocal")]
            public bool RetiraNoLocal { get; set; }
        }
  • did same post here with your object and worked normally not changed anything

  • How is that possible? What can that be? It doesn’t make sense that it works for you and not for me, does it have any configuration that I don’t have?

  • I found the error, Postman set to text

2 answers

1

You cannot receive the Objeto C# in the request parameter, you receive JSON(string) and then deserializes the same.

  • Can you give me a hand? can you help me one more time by showing me the way forward? I appreciate

  • Consultationservice Informationpersonal m = Jsonconvert.Deserializeobject<Consultationservice>(consultationAtendimento.Tostring()); would that be it? I’ll test

  • Barter ConsultaUnidadeAtendimentoModel for String in the method ConsultaUnidadeAtendimento

  • And the person who will send you it needs to send a valid JSON in relation to your object (as I had already explained in the other question)

  • Leonardo, without wanting to abuse more could post an example, it would be good to get registered, I believe other people would have the same problem one day

  • I found the error, Postman set to text

Show 1 more comment

1


tries to send JSON object changes in Postman inserir a descrição da imagem aqui

so you work with object becomes simpler

 public class Model
    {
        [JsonProperty("numeroCarrinho")]
        public long NumeroCarrinho { get; set; }

        [JsonProperty("itens")]
        public List<Carrinho> Itens { get; set; }

        [JsonProperty("cep")]
        public string Cep { get; set; }

        [JsonProperty("retiraNoLocal")]
        public bool RetiraNoLocal { get; set; }
    }

    public class Carrinho
    {
        [JsonProperty("codigo")]
        public string Codigo { get; set; }
        [JsonProperty("qtd")]
        public int Qtd { get; set; }
    }

json sent

 {
        "numeroCarrinho":122865,
        "itens":[
            {"codigo":"PA00058","qtd":1},
            {"codigo":"MA00068","qtd":1},
            {"codigo":"PA00004","qtd":1}
        ],
        "cep":"41706670",
        "retiradaNoLocal":false
    }
  • Thank you so much! now comes the trickiest part, see that I’m getting a dynamic data, the items, how I can catch them, now and the problem, if I can help thank you

  • need to know each one no better how is a field treat everything as string list and in the database save in a table cart items example

  • The problem is that comes the product code that would be the PA00058 and the quantity on the front

  • take a look I tested here worked right see if this meets your scenario

  • will need to change Json in this case

  • click to accept my answer there if it was useful

  • Ready! thanks !

Show 2 more comments

Browser other questions tagged

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