Api that receives an Item Array

Asked

Viewed 384 times

1

I have the following situation, I need to create an API that will receive the content below:

{
  "numeroCarrinho": 122865,
  "itens": {
    "PA00058": 1,
    "MA00068": 1,
    "PA00004": 1
  },
  "cep": "41706670",
  "retiraNoLocal": false
}

Example: localhost:7630/api/unit/query/cart/122865/PA00058:1,MA00068:1,PA00004:1/41706670/false

The problem and that the items would be dynamic, an array of items, as I could do this, I made an example more like to know how I can get all the separate items.

[HttpGet]
[Route("unidade/consulta/carrinho/{numerocarrinho}/{itens}/{cep}/{retiralocal}")]
public HttpResponseMessage ConsultaUnidadeAtendimento(string numerocarrinho, string[] itens, string cep, string retiralocal)
{

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

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

}

2 answers

0

You could create a class that represents the data to be received by the method, so your code becomes more readable. The data would be sent on the request body.

Class:

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

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

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

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

Controller:

 [Route("unidade/consultaUnidadeAtendimento")]
public HttpResponseMessage ConsultaUnidadeAtendimento(ConsultaUnidadeAtendimentoModel consultaAtendimento)
{

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

        return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
    }
}
  • Tomaz, thank you so much for your answer, I made the example, the detail and that the data posted are not coming, I opened a new question, if you can help I thank:https://answall.com/questions/261193/api-com-array-de-itens-n%C3%A3o-receives-the-data-posted

0

You can use the Dynamic

public JObject PostSavePage(dynamic testObject)
{
    return testObject;
}

would look like this :

inserir a descrição da imagem aqui

Update:

Complete Controller of Example:

using Newtonsoft.Json.Linq;
using System.Web.Http;

namespace TestApp.Controllers
{
    [RoutePrefix("api/values")]
    public class ValuesController : ApiController
    {
        [HttpPost]
        [Route("postsave")]
        public JObject PostSavePage(dynamic testObject)
        {
            return testObject;
        }
    }
}
  • Thanks Thiago, could you post your example code? thanks for the help

  • @itasouza done.. there inside the call there Oce manipulates the object , abs!

  • Thiago, thank you so much for your answer, I made the example, the detail and that the data posted are not coming, I opened a new question, if you can help I thank:https://answall.com/questions/261193/api-com-array-de-itens-n%C3%A3o-receives-the-data-posted

Browser other questions tagged

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