Post by Postman is giving error of Object Reference not set to an instance of an Object

Asked

Viewed 320 times

1

I understand that the Frombody is coming null and I don’t know why.

That’s my Postman payload

{
    "ChannelCode" : "TS",
    "Name" : "Teste",
    "Celphone" : "(11)999999999",
    "Endpoint" : "www.teste.com.br",
    "TokenLogin" : "1234567890",
    "TokenLoginExpiration" : "2018-06-13T00:00:00.000Z",
    "Active" : "true"
}

In this controller I get the request

[HttpPost]
[Authorize("Bearer")]
public async Task<IActionResult> Create([FromBody]ChannelCreateRequest channel)
{
     if (channel == null) throw new WhatsAppApiException("Favor informar os dados do Canal!");
     var result = await _channelCreateService.Process(new ChannelCreateCommand(channel.ChannelCode, channel.Name, 
                             channel.Celphone, channel.Endpoint, 
                             channel.TokenLogin,                                          
                             channel.TokenLoginExpiration ,                                                              
                             channel.Active.GetValueOrDefault(true)));

        return Ok(new ApiReturnItem<ChannelResult> { Item = result, Success = true });
    }

everything will be recorded on Mongodb

Channelcreaterequest class

public class ChannelCreateRequest
    {
        [JsonProperty("codigo_canal")]
        public string ChannelCode { get; set; }
        [JsonProperty("nome")]
        public string Name { get; set; }
        [JsonProperty("celular")]
        public string Celphone { get; set; }
        [JsonProperty("url")]
        public string Endpoint { get; set; }      

        //------ Remover-----------

        [JsonProperty("token_canal")]
        public string TokenLogin { get; set; }
        [JsonProperty("token_canal_expiracao")]
        public DateTime? TokenLoginExpiration { get; set; }

       //--------Fim remover---------

        [JsonProperty("ativo")]
        public Boolean? Active { get; set; }
    }

responding to Pagotti, this is Postman

inserir a descrição da imagem aqui

  • Usually this occurs when it fails to parse the body message for the object "Channelcreaterequest".

  • You can add the class code: Channelcreaterequest?

  • @Reiksiel, added

  • You debugged the code to see which point the exception is being thrown?

  • Object Reference not set to an instance of an Object.. All fields in Frombody are null. That is, I get nothing in the request.

  • Your Request comes with the content-type correct to report that it is a json?

Show 1 more comment

1 answer

1


Remove the "Jsonproperty" annotations looks like Webapi uses Newtonsoft as the default to deserialize the class and this is causing it to get lost.

Another solution is to use xml this way:

{
    "codigo_canal" : "TS",
    "nome" : "Teste",
    "celular" : "(11)999999999",
    "url" : "www.teste.com.br",
    "token_canal" : "1234567890",
    "token_canal_expiracao" : "2018-06-13T00:00:00.000Z",
    "ativo" : "true"
}
  • Actually the name of the properties in the Mongo document should be the one that json serializes and not their real name. That’s why NULL came, he didn’t know those properties. Thanks to all.

Browser other questions tagged

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