-1

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:

enter image description here

"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:

enter image description here

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.

https://stackoverflow.com/a/983458/3850405

Ogglas
  • 38,157
  • 20
  • 203
  • 266
  • @mjwills I would like our current infrastructure to cache the values, this is not happening for POST request. – Ogglas Mar 13 '19 at 10:31
  • did you tried: https://localhost:4431/api/test/dictionary?test=[{"2019":"30"}] ? – isaeid Mar 13 '19 at 10:35
  • @mjwills Worked perfectly, marked as duplicate now. I tried searching before posting but I did not find that thread. Thanks! – Ogglas Mar 13 '19 at 10:41

1 Answers1

0

@mjwills duplicate solved it for me:

https://localhost:4431/api/test/dictionary?test[0].key=2019&test=[0].value=30.09164818893

Ogglas
  • 38,157
  • 20
  • 203
  • 266