I have a problem sending a Dictionary<int, decimal>
as GET URI request. I can easily send the request with values like this in a POST request body and make it work:
"test": {
"2019": 30.0916481889
}
https://stackoverflow.com/a/34030142/3850405
However since I'm not creating or modifying anything I wan't to use the correct HTTP method for clarity. As you can see above JsonFormatter
does not bind a Dictionary
from Javascript Array
and that is why a object needs to be used.
This is the method I'm using. I have tried to omit [FromUri]
but then the value is always null. Also tried using Dictionary<int, int>
and Dictionary<string, string>
to prevent culture differences.
[Route("api/test/dictionary")]
[HttpGet]
public async Task<IHttpActionResult> TestDictionary([FromUri]Dictionary<int, decimal> test){
}
Requests that I have tried, all combinations have been tried with and without URL encoding:
https://localhost:4431/api/test/dictionary?test=%7B%222019%22%3A%2230.0916481889%22%7D
https://localhost:4431/api/test/dictionary?test=%22test%22%3A%7B%222019%22%3A%2230.0916481889%22%7D
https://localhost:4431/api/test/dictionary?test="test":{"2019":"30"}
https://localhost:4431/api/test/dictionary?test={"2019":"30"}
https://localhost:4431/api/test/dictionary?test={"2019":30}
https://localhost:4431/api/test/dictionary?test={2019:30}
https://localhost:4431/api/test/dictionary?test=2019:30
https://localhost:4431/api/test/dictionary?test=2019
https://localhost:4431/api/test/dictionary?test=2019&test=30 (Longshot from how arrays are handled https://stackoverflow.com/a/11100414/3850405)
Looks like this when request is received:
How can a dictionary be sent as a GET URI request? Even if RFC2616 has deleted the sentence the message-body SHOULD be ignored when handling the request
for GET requests my experience is that it can cause problems in larger systems. Caching/Proxies not working as expected to name a few quirks that can easily happen and are often a pain to debug.