I'm trying to POST a data to web using HttpClient but i can't succeed.
Here is my JSON web api
{
"Categories":[
{
"CategoryID":1,
"Category":"Category 1"
},
{
"CategoryID":2,
"Category":"Category 2"
}
]
}
i'am sending categories data to web my web developer send me above json to send a data from winform to web
Here is my code
IEnumerable<KeyValuePair<string, string>> paramt = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string,string>("CategoryID","1"),
new KeyValuePair<string,string>("Category","Pizza")
};
HttpContent q = new FormUrlEncodedContent(paramt);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api);
HttpResponseMessage response = client.PostAsync("api/categories", q).Result;
}
sorry for my english moderator please update my question
(moderator)are moderators. The rest of us are just users like yourself. 2. You haven't described the problem and why your code isn't working. You have only described what you're trying to do, not why you've posted a question. Despite this, I think I understand the issue, as I've described above. The linked duplicate shows you how to post JSON (as opposed to a form post) as you're currently doing.