Post by postman is giving Object reference error not set to an instance of an object

1

I notice that FromBody is coming null and I do not know why.

This is my payload on Postman

{
    "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 saved in 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, here's the postman

    
asked by anonymous 14.06.2018 / 16:15

1 answer

1

Remove "JsonProperty" annotations It 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 like this:

{
    "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"
}
    
14.06.2018 / 17:08