How to Set Up Httpcontent for My Httpclient Postasync Second Parameter

How do I set up HttpContent for my HttpClient PostAsync second parameter?

This is answered in some of the answers to Can't find how to use HttpContent as well as in this blog post.

In summary, you can't directly set up an instance of HttpContent because it is an abstract class. You need to use one the classes derived from it depending on your need. Most likely StringContent, which lets you set the string value of the response, the encoding, and the media type in the constructor. See: http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx

How do I set up HttpContent for my HttpClient PostAsync second parameter?

This is answered in some of the answers to Can't find how to use HttpContent as well as in this blog post.

In summary, you can't directly set up an instance of HttpContent because it is an abstract class. You need to use one the classes derived from it depending on your need. Most likely StringContent, which lets you set the string value of the response, the encoding, and the media type in the constructor. See: http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx

PostAsync with two strings using HttpClient and HttpResponseMessage

Figured it out. Thanks to CrowCoder for his post.

I ended up making a Dictionary which contained my values.

public async static Task<string> ConvertFiles(string s1, string s2, string webAddress)
{
Dictionary<string, string> jsonValues = new Dictionary<string, string>();
jsonValues.Add("string1", "anyStringValue1");
jsonValues.Add("string2", "anyStringValue2");

HttpClient client = new HttpClient();
StringContent sc = new StringContent(JsonConvert.SerializeObject(jsonValues), UnicodeEncoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(webAddress, sc);
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine("Result: " + content);
return content;
}

Now you just need to use a dynamic type to access string1 and string2 like this:

[HttpPost("/mshdf/import")]
public string ConvertFiles([FromBody]dynamic jsonObject)
{
Console.WriteLine("string1: " + jsonObject.string1);
Console.WriteLine("string2: " + jsonObject.string2);
return "woo";
}

Add Params HttpClient C#

When you format your URL you need to add the parameters like so:

var url = string.Format("{0}{1}", ServicePrefix, controller);

url = string.Format("{0}?token=123456", url);

Note the ? between the URL and the query parameter.

You don't specify how you get the value for token into your method but, if it is a readonly value similar to ServicePrefix you can pass it as a parameter to string.Format:

var url = string.Format("{0}{1}", ServicePrefix, controller);

url = string.Format("{0}?token={1}", url, Token);

You can always put this on one line, but I have split it to make it easier to read :-)

HttpClient headers vs HttpContent headers

HttpClient supports several types of content. For example:

  • System.Net.Http.ByteArrayContent
  • System.Net.Http.Json.JsonContent
  • System.Net.Http.MultipartContent
  • System.Net.Http.ReadOnlyMemoryContent
  • System.Net.Http.StreamContent

For a complete list of supported content, see HttpContent.

HttpContent contains some more specific headers about the content, including the content type.

I think this list here can give you a pretty good understanding on what headers that are available. I do agree that having jus one set of headers would make things much easier.



Related Topics



Leave a reply



Submit