Categories
ASP.NET Development JSON

ASP.NET 3.0. Date in seconds cannot be converted from JSON to System.Date within POST request

I want to test my POST endpoint with simple request with JSON in its body.

I have an ASP.NET Core 3.0 project with one single controller and one endpoint inside it. I created empty ASP.NET Core project primarily.
Then I created a simple controller in order to process POST requests.


    [ApiController]
    [Route("api/[controller]")]
    public class UpdateController : Controller
    {
        // POST api/update
        [HttpPost]
        public async Task<IActionResult> Post(Update update)
        {
            return Ok();
        }
    }

Update class is a type from Telegram.Bot.Types

When I try to test my POST action method with following data in a body:


    {
    "update_id":10000,
    "message":{
      "date":1441645532,
      "chat":{
         "last_name":"Test Lastname",
         "id":1111111,
         "first_name":"Test",
         "username":"Test"
      },
      "message_id":1365,
      "from":{
         "last_name":"Test Lastname",
         "id":1111111,
         "first_name":"Test",
         "username":"Test"
      },
      "text":"/start"
      }
    }

I get the following error:
The JSON value could not be converted to System.DateTime. Path: $.message.date | LineNumber: 3 | BytePositionInLine: 19.

But I expected it would work fine.

I’ve already checked Message type in Telegram’s repository. Message type belongs to Update one as a property.

And date there is marked with UnixDateTimeConverter:

    [JsonProperty(Required = Required.Always)]
    [JsonConverter(typeof(UnixDateTimeConverter))]
    public DateTime Date { get; set; }

I’ve also tried to deserialize JSON from above using Newtonsoft.JSON and it did right without any errors. But still have a problem to get it done via ASP.NET Core 3.0 project.

Leave a Reply

Your email address will not be published. Required fields are marked *