StringContent vs ObjectContent

I am using System.Net.Http’s HttpClient to call a REST API with “POST”
using the following code:

using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(request)) { })
{
     response = await httpClient.PostAsync(request, objectContent);
}

The “objectContent” is currently this –

objectContent = new ObjectContent(jsonContent.GetType(), jsonContent, new JsonMediaTypeFormatter());

I was wondering what difference it makes if this was a StringContent rather than an ObjectContent like this?

objectContent = new StringContent(content);
objectContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

Both work fine. Because it is JSON, i tend to assume that StringContent would make sense. But when is ObjectContent to be used because pretty much all content sent is a “string”.


Source: json

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.