Earn income with your C# skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

How to set the Content-Type header for an HttpClient request

The content type can be specified when creating the request content itself.

Note that the example below adds 'application/json'' in two places -- for Accept and Content-Type headers.

var client = new HttpClient();
client.BaseAddress = new Uri("http://example.com/");

// ACCEPT header

     new MediaTypeWithQualityHeaderValue("application/json"));

var request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");

// CONTENT-TYPE header

request.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}", 
                     Encoding.UTF8, "application/json");

client.SendAsync(request).ContinueWith(responseTask => 
        Console.WriteLine("Response: {0}", responseTask.Result); );

Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas.      Vsn 1.3.0