Sending & Receiving JSON With A HttpClient In C#
[C#, HttpClient]
In my previous post, Sending a HTTP HEAD Request With A HttpClient In C#, I talked about how to send various requests using a HttpClient.
Of interest is this part where we prepare content for submission (for POST and PUT requests)
// Sample request
var payload = new { Name = "James Bond", Age = 50 };
// Serialize
string jsonPayload = JsonSerializer.Serialize(payload);
// Create a StringContent object with the serialized JSON.
// The content does not have to be JSON!
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
This is such a common use case that convenience methods were added to the HttpClient
.
If you want to POST JSON, you do it as follows:
// Sample request
var payload = new { Name = "James Bond", Age = 50 };
// Sample post
var response = client.PostAsJsonAsync("URL", payload);
To PUT there is a corresponding method:
// Sample request
var payload = new { Name = "James Bond", Age = 50 };
// Sample put
var response = client.PutAsJsonAsync("URL", payload);
If you wanted to receive JSON, the hard way it so deserialize the response yourself.
Assuming we had this record:
public record Person(string Name, string Age);
We could fetch and deserialize a Person
like this:
// Get the HttpResponse
var response = await client.GetAsync("URL");
// Fetch the content from the response
var personString = await response.Content.ReadAsStringAsync();
// Deserialize into a person
var person = JsonSerializer.Deserialize<Person>(personStrin
This is such a common use case, a helper has been introduced for reading the response.
The same result can be achieved as follows:
// Get the HttpResponse
var response = await client.GetAsync("URL");
// Deserialize the content
var person = await response.Content.ReadFromJsonAsync<Person>();
This has been simplified even further at the HttpClient
level.
var person = await client.GetFromJsonAsync<Person>("URL");
Happy hacking!