I'm having an error :
Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from '
System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>' to 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>
When I am posting nested dictionary using HttpClient
var arg_employee = new Dictionary<string, Dictionary<string, string>>
{
{
"filter_data",
new Dictionary<string, string>
{
{"user_name", "admin"},
}
},
};
var content = new FormUrlEncodedContent(arg_employee);
// ... Use HttpClient.
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.PostAsync(uRL, content))
using (HttpContent responseContent = response.Content)
{
// ... Read the string.
return await responseContent.ReadAsStringAsync();
How I can post nested dictionary or array ?
FormUrlEncodedContent accepts IEnumerable<KeyValuePair<string, string>> nameValueCollection , not Dictionary. See this
To pass dictionary you should serialize it to json, for example.
First step, getting json representation of your dictionary using Newtonsoft.Json (you can manually serialize it or use another library)
using Newtonsoft.Json
var arg_employee = new Dictionary<string, Dictionary<string, string>>
{
{
"filter_data",
new Dictionary<string, string>
{
{"user_name", "admin"},
}
},
};
var jsonDictionary = JsonConvert.SerializeObject(arg_employee );
Second step, post it as StringContent instead of FormUrlEncodedContent:
var content = new StringContent(jsonDictionary , Encoding.UTF8, "application/json");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With