Problems when passing an object in an Httppost request

Asked

Viewed 92 times

0

I’m creating a Dotnet API, and when I tried to test a Post request with Postman the only data coming to my controller’s method is Name and CPF.

Below is an image with the request I’m making in Postman

inserir a descrição da imagem aqui

Below the class of the object I am passing via Post.

using System.ComponentModel.DataAnnotations;
namespace PimVIIIAPI.Model
{
   public class Pessoa
   {
      [Key]
      protected int Id { get; set; }
      [Required(ErrorMessage = "Informe o nome")]
      public string Nome { get; set; }
      [Required(ErrorMessage = "Informe o cpf")]
      public long Cpf { get; set; }
      [Required(ErrorMessage = "Informe o endereço")]
      public Endereco Endereco = new Endereco();
      public Telefone[] Telefone = new Telefone []{new Telefone()};
   }
}

I cannot understand why all data is not being received.

inserir a descrição da imagem aqui

I believe I have created the request correctly and passed the object in the correct way.

2 answers

1


You’re probably not succeeding because you need to deseralize the content you receive through the request. Your controller’s method would look something like:

InserirPessoa(dynamic args){
    Pessoa objPessoa;
    try
    {
        objPessoa =
            new Pessoa()
            {
                Nome = args.Nome,
                CPF = args.CPF,
                Endereco = JsonConvert.DeserializeObject<Endereco>(args.Endereco),
                Telefone = JsonConvert.DeserializeObject<List<Telefone>>(args.Telefone),
            };
    }
    catch
    {
        throw;
    }

    //salvar
}

I hope I’ve helped..

  • I get it, I’m taking this test today, thank you......

  • It didn’t work I’ll keep trying, in the case of the dynamic object I received the data correctly but I can’t pass them to my object of type Person the way you passed me the example.

0

After trying hard and based on the hint presented I was able to solve the problem with another question I just ran into right here in stackoverflow.

And I ended up implementing the following solution in my controller.

[HttpPost("InsertPessoa/{pessoa}")]
    public bool InsertPessoa(object pessoa)
    {

        dynamic m = JsonConvert.DeserializeObject(pessoa.ToString());
        Pessoa p = new Pessoa();

        try
        {
            p.Nome = m.Nome;
            p.Cpf = m.Cpf;
            p.Endereco.Logradouro = m.Endereco.Logradouro;
            p.Endereco.Numero = m.Endereco.Numero;
            p.Endereco.Cep = m.Endereco.Cep;
            p.Endereco.Bairro = m.Endereco.Bairro;
            p.Endereco.Cidade = m.Endereco.Cidade;
            p.Endereco.Estado = m.Endereco.Estado;

            p.Telefone[0].Ddd = m.Telefone[0].Ddd;
            p.Telefone[0].Numero = m.Telefone[0].Numero;
            p.Telefone[0].Tipo.Tipo = m.Telefone[0].Tipo.Tipo;


        }
        catch(Exception er)
        {
            throw new Exception(er.Message);
        }
        return PessoaDAO.Insira(p);
    }

In this way I was able to receive the Json data correctly.

My many thanks to

Juliano Roberto

For taking the time and giving me that strength of response here, which helped me find exactly what I needed.

Browser other questions tagged

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