I\'m trying to set the Content-Type
header of an HttpClient
object as required by an API I am calling.
I tried setting the Content-Ty
If you don't mind a small library dependency, Flurl.Http [disclosure: I'm the author] makes this uber-simple. Its PostJsonAsync
method takes care of both serializing the content and setting the content-type
header, and ReceiveJson
deserializes the response. If the accept
header is required you'll need to set that yourself, but Flurl provides a pretty clean way to do that too:
using Flurl.Http;
var result = await "http://example.com/"
.WithHeader("Accept", "application/json")
.PostJsonAsync(new { ... })
.ReceiveJson();
Flurl uses HttpClient and Json.NET under the hood, and it's a PCL so it'll work on a variety of platforms.
PM> Install-Package Flurl.Http