peter
peter

Reputation: 15109

DateTime parses differently when passed via HTTP-POST in body and via HTTP-GET as query params

On our MVC 3 Web Application we define the Thread.CurrentThread.CurrentCulture with the current user's preferences. However, in one View we submit the data via Ajax always in the format mm/dd/yyyy this works fine as long as the user's culture is en_US. (obviously)

The surprising thing, it works for all users, when we pass it as HTTP-POST, but stops working when we pass it as HTTP-GET. (In both cases, the exact same value is leaving the browser).

Any ideas why HTTP-POST calls would be handled differently than HTTP-GET?

Also, there are other parameters to that action. In case of an HTTP-POST, they are all set in the body like:

{
  "date": "04/15/2016",
  "someText": "Hello World",
  "someNumber": 42
}

and for HTTP-GET they are query parameters instead:

domain.tld/controller/action?date=04%2F15%2F2016&someText=Hello+World&someNumber=42

Update:

To avoid confusion, the method is called correctly and returns values in both cases. It's just that the DateTime param (which is actually DateTime?) isn't being set, just the same as if I'd have set it to some invalid date / string.

Upvotes: 0

Views: 826

Answers (2)

kakurala
kakurala

Reputation: 824

Can you please check whether your request handler is GET or POST, if it was set to POST then the GET request can't be processed.

edit ** Ignore below one ** : since URL encoding is same in both methods

"The only difference that in GET request is all the special characters will be encoded for. ex / to %2f where in POST it wont happen."

Upvotes: -1

heymega
heymega

Reputation: 9401

Any ideas why HTTP-POST calls would be handled differently than HTTP-GET?

The default model binder uses the following cultures depending on the HTTPVERB

  • InvariantCulture for your HTTPGET
  • CurrentCulture for your HTTPPOST requests.

This is because users will share their URLS (HTTPGET) with other users from different cultures.

Imagine we have a user from America who wants to send a URL to their friend in the United Kingdom about an event taking place on 04/15/2016. (15th April 2016). They give their friend the following URL...

http://www.myurl.com/tickets/?eventdate=04%2F15%2F2016

If we didn't use Invariant Culture then this date would not be valid for our friend in the UK since their culture expects dd/mm/yyyy.

With regards to HTTPPOST, Users will not typically share these types of requests with other users as they are generally generated on a per user basis. Therefore we can explicitly use the CurrentCulture.

You can override this default behaviour by creating your own model binder.

Hope this helps!

Upvotes: 3

Related Questions