skip to Main Content

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.

2

Answers


  1. You can try the following:

    [JsonConverter(typeof(JavaScriptDateTimeConverter))]
    

    Do a double dataannotation otherwise you would have to write your own custom converter.

    Login or Signup to reply.
  2. From ASP.NET Core 3.0, Json.NET is no longer included as the default JSON (de)serializer, that role has been given to System.Text.Json.

    In a lot of cases this will be sufficient. System.Text.Json cannot be used instead of Json.NET in all cases though – this is on purpose as it is supposed to be lightweight. However you can still use Json.NET in ASP.Net Core 3.0 if System.Text.Json just doesn’t cater for your needs.

    Form Immo Landwerth’s blog post Try the new System.Text.Json APIs there’s a section on Integration with ASP.NET Core MVC

    If you’d like to switch back to the previous default of using Newtonsoft.Json, do the following:

    1. Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package.
    2. In ConfigureServices() add a call to AddNewtonsoftJson()
    public void ConfigureServices(IServiceCollection services)
     {
         ...
         services.AddControllers()
    +            .AddNewtonsoftJson()
         ...
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search