patterncsharpCritical
POSTing JsonObject With HttpClient From Web API
Viewed 0 times
withjsonobjectwebfromapihttpclientposting
Problem
I'm trying to POST a
Here's what I have so far:
I think I need to cast my
JsonObject using HttpClient from Web API. I'm not quite sure how to go about this and can't find much in the way of sample code. Here's what I have so far:
var myObject = (dynamic)new JsonObject();
myObject.Data = "some data";
myObject.Data2 = "some more data";
HttpClient httpClient = new HttpClient("myurl");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.Post("", ???);I think I need to cast my
JsonObject as a StreamContent but I'm getting hung up on that step.Solution
With the new version of
Or if you want it
HttpClient and without the WebApi package it would be:var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
var result = client.PostAsync(url, content).GetAwaiter().GetResult();Or if you want it
async:var result = await client.PostAsync(url, content);Code Snippets
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
var result = client.PostAsync(url, content).GetAwaiter().GetResult();var result = await client.PostAsync(url, content);Context
Stack Overflow Q#6117101, score: 703
Revisions (0)
No revisions yet.