C# - How to make HTTP / REST calls using the HttpClient

C# has great built-in support for making HTTP requests using the HttpClient. Using this client you can Put, Post, Get, Delete and much more using your favourite formats whether that is JSON or XML.

Using the HttpClient with a simle Get

You can make a simple get request with the following piece of code:

var httpClient = new HttpClient();
var response = await httpClient.GetAsync("https://google.com");

In the above we create a new HttpClient and make HTTP Get call to fetch a page at ´google.com´, that is as simple as it gets. Using the response we can get a successful response code using var isSuccessful = response.IsSuccessStatusCode;. You should not dispose your httpClient but try to reuse them as much as possible - see this post for more info.

Putting and posting JSON

You can also use the HttpClient to put JSON using the PutJson method:

var myObject = new SomeObject
{
   SomeProperty = "someValue"
};

var objAsJson = JsonConvert.SerializeObject(myObject);
var content = new StringContent(objAsJson, 
   Encoding.UTF8, "application/json");
var httpClient = new HttpClient();
var result = await httpClient.PutAsync(
   "http://someDomain.com/someUrl", content); //or PostAsync for POST

In the above we make an object that we then serialise to a JSON string using JsonConvert, from there we use the PutAsync method on the httpClient to send it to a certain URL. That is all you need to go from having an object to call and endpoint with it as JSON. You can see my full post on putting or posting JSON here.

More advanced - Other verbs

Sometimes you need something a little more advanced than your average Get, Put or Post of JSON. Below is an example of sending a Post request with XML as body:

var httpClient = new HttpClient();
var someXmlString = "<SomeDto><SomeTag>somevalue</SomeTag></SomeDto>";
var stringContent = new StringContent(someXmlString, Encoding.UTF8, "application/xml");
var response = await httpClient.PostAsync("/someurl", stringContent);

The above is not just an example of posting XMl in C#, but also how to use StringContent to send any format depending on the content type - which is application/xml in the above.

Setting headers

You can also set headers for each request using .Headers.Add():

var httpClient = new HttpClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, 
   "http://localhost:58116/weatherforecast");
requestMessage.Headers.Add("MyFantasticHeader", 
   "MyFantasticValue");
var result = await httpClient.SendAsync(requestMessage);

In the above we add the "MyFantasticHeader" header with the value "MyFantasticValue". Another great feature of the HttpClient is that you can set base headers, these are headers that will be used for every request, an example of this is seen below:

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("MyFantasticHeader"
   ,"MyFantasticValue");

Setting a base address

You can also set a base address on the HttpClient so that every request will use the same base URL:

var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://peterdaugaardrasmussen.com/");
var response = await httpClient.GetAsync("about/");

This is handy if you want a HttpClient for every domain you want to call.

That is it!

That is all I want to write about the HttpClient, let me know if I missed some important details, in the comments down below!