1

Using Reactjs with Axios to post/get to a dotnetcore webapi.

  1. Axios GET api call to retrieve a list of datetimesUTC
    • Response datetimeUTCs are clearly correct in console/display.
  2. Axios GET api call passes in the selected datetimeUTC
    • The API clearly receives the correct UTC datetime
  3. Axios POST api call passes in the same selected datetimeUTC
    • The API receives localized datetime instead of the UTC time.

My issue is: Why in the world does my API get the correct UTC on a GET, but localized on a POST?

I have tried an empty controller functions, one with POST, one with GET, and can clearly see they are different.

/// API post w/ Axios
axios.post("api/action", {
  date: date
});
/// API get w/ Axios
axios.get("api/action", {
  params: { date: date}
});
[HttpPost("{action}")]
public void Reconcile([FromBody]DateTime date)
{
  ...
}
[HttpGet("{action}")]
public void Reconcile(DateTime date)
{
  ...
}

The GET api action receives the correct UTC datetime. The POST api action receives the incorrect, localized datetime.

EDIT:

  • POST

    • Axios Post: date: "2019-01-01T12:00:00.000Z"
    • API received: DateTime - {1/1/2019 12:00:00 PM}
  • GET

    • Axios Get: date: "2019-01-01T12:00:00.000Z"
    • API received: DateTime - {1/1/2019 5:00:00 AM}

I just cannot fathom why it changes for post/get. I didn't have to do anything fancy at all for the GET, but it's a nightmare all day trying to get the POST to work. I could just change it to a GET and it'd be fine, but semantically it really ought to be a POST.

Igor
  • 60,821
  • 10
  • 100
  • 175
  • when you post its a whole object with a `date` property. – Daniel A. White Apr 23 '19 at 19:08
  • @DanielA.White What can I do to fix it though? – DateTimeSucks Apr 23 '19 at 19:11
  • you have to write a model class that has a date property. – Daniel A. White Apr 23 '19 at 19:12
  • Check the value being passed to the controller in the network tab of your browser in the developer tools. I am willing to bet there is either no time offset specified *or* there is no trailing `Z` which indicates UTC. I have to guess here because you did not include the actual **values** being sent/tested, if you want more help please include these (ie. actual value being sent and actual value received). – Igor Apr 23 '19 at 19:13
  • It's a function of the `DateTime` conversion to/from JSON. If I had to guess, I'd say it specifically has to do with losing the `DateTimeKind` value. There's probably a workaround of some sort, but I'd recommend using `DateTimeOffset` instead, which is better anyways. – Chris Pratt Apr 23 '19 at 19:15
  • Check the `DateTime.Kind` value on the received dates. I'm going to guess that one is set to `DateTimeKind.Local` and the other to `DateTimeKind.Utc` – Igor Apr 23 '19 at 20:06
  • The DateTimeKind is Local for Get, and Utc for Post. – DateTimeSucks Apr 23 '19 at 20:09
  • 1
    Ok, so the values are actually the same then for the received instances. You could call `date.ToUniversalTime()` to get the Utc equivalent DateTime instance. As to what is changing the received utc datetime to a local date time that would be something in the binding or deserialization, I do not know enough about asp.net-core-webapi to say where specifically to start looking though. – Igor Apr 23 '19 at 20:11
  • Although check this quesiton: [ISO UTC DateTime format as default json output format in MVC 6 API response](https://stackoverflow.com/a/41728953/1260204) – Igor Apr 23 '19 at 20:14
  • @Igor The date is the same, but the time is different. – DateTimeSucks Apr 23 '19 at 20:14
  • 1
    `The date is the same, but the time is different` <= No, both instances have an equivalent value. The server can correctly convert a DateTime instance between local and utc based on the DateTimeKind flag. – Igor Apr 23 '19 at 20:16
  • I added the JsonOptions from that SO post, and now my GET is: - Get params: "2019-01-08T04:59:59.000Z" - Get api receives: "{12/31/2018 10:00:00 PM}" I just can't win. – DateTimeSucks Apr 23 '19 at 20:34
  • This guy has the same issue, still unsolved: https://stackoverflow.com/questions/25728555/net-webapi-inconsistent-datetime-kind-post-vs-get – DateTimeSucks Apr 23 '19 at 20:52

0 Answers0