0

I have POST endpoint which receives an object with DateOnly field. I've tried to use https://github.com/maxkoshevoi/DateOnlyTimeOnly.AspNet package but didn't resolve my issue. still getting The JSON value could not be converted to DateOnly error

this is the payload :

[
  {
    "startDate": {
      "year": 2023,
      "month": 5,
      "day": 29,
      "dayOfWeek": 1
    },
    "name": "string",
    "endDate": {
      "year": 2023,
      "month": 11,
      "day": 1,
      "dayOfWeek": 2
    }
  }
]

and POST method :

[HttpPost("Save")]
public async Task<IActionResult> Save([FromBody] IEnumerable<Data> Data)
{
       // some logic

        return Ok();
}
Steven
  • 166,672
  • 24
  • 332
  • 435
lolo xoxo
  • 31
  • 2
  • 1
    Welcome to SO. You might find reading the site [help] useful when it comes to [ask], and this question [checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). Code that you've worked on to solve the problem should include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), and **be included** in your question. – Felix May 29 '23 at 17:34
  • I bet that ASP.NET Core can't parse `{ "year": 2023, "month": 5, "day": 29, "dayOfWeek": 1 }` to a date. ASP.NET Core requires the date to be in standard ISO notation. Rather try this: `"startDate": "2023-05-29"`. – Steven May 29 '23 at 17:41
  • I updated my question with sample payload and HTTP endpoint – lolo xoxo May 29 '23 at 17:41
  • @Steven Swagger creates a request model based on my POST method body. It was like that in swagger – lolo xoxo May 29 '23 at 17:42
  • 1
    I think this is a bug in Swagger. Your version of Swagger doesn't support `DateOnly` yet, which is why it outputs the `Year`, `Month`, `Day`, and `DayOfWeek` properties of `DateOnly`. Check to see if there's a newer version of Swagger that supports `DateOnly` and otherwise simply ignore the Swagger example. – Steven May 29 '23 at 17:49
  • Can you save the Date as a string in Json? [This link can help you](https://stackoverflow.com/questions/3477735/how-to-convert-datetime-to-from-specific-string-format-both-ways-e-g-given-fo) – AyobKafrashian Jun 07 '23 at 11:52

0 Answers0