For some reason I can never remember how to make a POST or PUT request with a content-type other than JSON or XML. For JSON and XML .Net core has extension methods for the HttpClient, which means you can do the following:
var httpClient = new HttpClient();
var jsonAsString = "{ \"text\":\"Some text\"}";
var response = await httpClient.PostAsJsonAsync("/someurl", jsonAsString);
The above can also take an object as parameter instead of a string, however sometimes you would like to post something different than XML or JSON. In this case you need to use the StringContent
class and provide it with a content-type, below is an example of this:
var httpClient = new HttpClient();
var content = new StringContent("This is plain text!", Encoding.UTF8, "text/plain");
var response = await httpClient.PostAsync("/someurl", content);
The equivalent and a verbose way to do the same using JSON would be the following (which is unnecessary due to the extension method in the first code block):
var httpClient = new HttpClient();
var jsonAsString = "{ \"text\":\"Some text\"}";
var jsonContent = new StringContent(jsonAsString, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("/someurl", jsonContent);
That is it
I hope this helped you, let me know in the comments if it did, or if you know an easier way!