How to host JSON with HttpClient?

I don't know how POST JSON is with HttpClient,

I find some solution

Like this

But I have to use HttpClient,

reason async and add a title,

this is my code below

any idea how to fix it?

List<Order> list = new List<Order> { new Order() { Name = "CreatedTime", OrderBy = 1 } }; Queues items = new Queues { Orders = list }; var values = new Dictionary<string, string> { { "Orders", JsonConvert.SerializeObject(list) } }; var content = new FormUrlEncodedContent(values); //HttpContent cc = new StringContent(JsonConvert.SerializeObject(items)); _msg = await _client.PostAsync(input, content); //_msg = await _client.PostAsync(input, cc); var response = await _msg.Content.ReadAsStringAsync(); 
+5
source share
1 answer

You can use the PostAsJsonAsync method, which can be found in extension builds:

 System.Net.Http.Formatting.dll 

Example

 public static async Task SendJsonDemo(object content) { using(var client = new HttpClient()) { var response = await client.PostAsJsonAsync("https://example.com", content); } } 

If you want to add custom headers to the request, add it to DefaultRequestHeaders :

 client.DefaultRequestHeaders.Add("mycustom", "header1"); 
+7
source

Source: https://habr.com/ru/post/1213195/


All Articles