256

I am trying to use HttpContent:

HttpContent myContent = HttpContent.Create(SOME_JSON);

...but I am not having any luck finding the DLL where it is defined.

First, I tried adding references to Microsoft.Http as well as System.Net, but neither is in the list. I also tried adding a reference to System.Net.Http but the HttpContent class is not available.

So, can anyone tell me where I can find the HttpContent class?

Youngjae
  • 24,352
  • 18
  • 113
  • 198
user1416156
  • 2,973
  • 4
  • 18
  • 14

6 Answers6

467

Just use...

var stringContent = new StringContent(jObject.ToString());
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

Or,

var stringContent = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);
Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • Says the content parameter needs to be IHttpContent and not StringContent. When I cast it to the interface it's happy, though. – micahhoover May 13 '15 at 01:39
  • 3
    Ended up using HttpStringContent. I couldn't use the StringContent class because PostAsync (or PutAsync in my case) doesn't accept StringContent even if you cast it to a IHttpContent object. – micahhoover May 13 '15 at 02:03
  • 4
    StringContent works for me with PostAsync, but if you want or need to use HttpContent, you can do so like this: HttpContent content = new StringContent(jsonString); – Chris Koester Aug 28 '17 at 20:58
  • I keep seeing this answer to what appears to be a similar problem to mine, however as I debug through both of my APIs, I get a PostAsync("path", StringContent) to fire but when it hits the other API I don't have a body to parse and use and on return I get a 500... I am at a loss as it appears I am doing it just like this. Only difference is that I don't serialize the object as it is already serialized when I receive it in the first API and it is a simple passthrough. – Slagmoth Aug 14 '19 at 17:07
  • 2
    This should be the answer :/ – DanielV Nov 27 '19 at 13:30
  • @DanielV No, that should not be an answer to question '*Can't find how to use HttpContent*' without explaining anything. – Oak_3260548 Nov 01 '21 at 14:48
172

To take 6footunder's comment and turn it into an answer, HttpContent is abstract so you need to use one of the derived classes:

enter image description here

Chris S
  • 64,770
  • 52
  • 221
  • 239
  • 11
    Looks like he used Visual Studio's "Class Diagram" feature (Right-click your project, Add Item, Class Diagram. Then you can go thru Solution Explorer and expand References to get diagrams of libraries you reference.) https://learn.microsoft.com/en-us/visualstudio/ide/working-with-class-diagrams-class-designer – Arin Jul 05 '17 at 22:43
  • @Chris, Please explain how you generated this class diagram for HttpContent... – Robin Gupta Sep 15 '21 at 01:49
128

For JSON Post:

var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

Non-JSON:

var stringContent = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("field1", "value1"),
    new KeyValuePair<string, string>("field2", "value2"),
});
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

https://blog.pedrofelix.org/2012/01/16/the-new-system-net-http-classes-message-content/

Felipe Deveza
  • 1,899
  • 3
  • 19
  • 32
6

While the final version of HttpContent and the entire System.Net.Http namespace will come with .NET 4.5, you can use a .NET 4 version by adding the Microsoft.Net.Http package from NuGet

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
0

I'm pretty sure the code is not using the System.Net.Http.HttpContent class, but instead Microsoft.Http.HttpContent. Microsoft.Http was the WCF REST Starter Kit, which never made it out preview before being placed in the .NET Framework. You can still find it here: http://aspnet.codeplex.com/releases/view/24644

I would not recommend basing new code on it.

RasmusW
  • 3,355
  • 3
  • 28
  • 46
-2

Just leaving the way using Microsoft.AspNet.WebApi.Client here.

Example:

var client = HttpClientFactory.Create();
var result = await client.PostAsync<ExampleClass>("http://www.sample.com/write", new ExampleClass(), new JsonMediaTypeFormatter());