I'm sending a dictionary with a call such as the following:
public async Task<string> Test(string val1, string val2)
{
string res = null;
using (HttpClient http = new HttpClient())
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("value1", val1);
dic.Add("value2", val2);
string data = JsonHelper.Serialize<Dictionary<string, string>>(dic);
using (StringContent content = new StringContent(data, Encoding.UTF8, "application/json"))
{
Uri uri = new Uri("http://www.whatever.com/api/test");
using (HttpResponseMessage response = await http.PostAsync(uri, content))
{
res = await response.Content.ReadAsAsync<string>();
}
}
}
return res;
}
and I see that on the receiving side the dictionary get always deserialized as empty.
[HttpPost]
public IHttpActionResult Test(Dictionary<string, string> req)
{
return Ok();
}
What am I doing wrong?
If I try to get the body of the request (string body = await Request.Content.ReadAsStringAsync();
) and deserialied it manually using a DataContractJsonSerializer I get the expected Dictionary.