C# - How to put or post XML using the HttpClient in .Net Core

It is quite easy to send XML using the HttpClient in asp.net core. In order to do this you need to use the StringContent object, provide it with an XML string, an encoding format and a mediatype. The XML string provided will form the body of the HTTP request. A full example on how to do this can be seen below:

var httpClient = new HttpClient();
var someXmlString = "<xml><someTag>someValue</someTag></xml>";
var stringContent = new StringContent(someXmlString, Encoding.UTF8, "application/xml");
var respone = await httpClient.PostAsync("/someurl", stringContent);

The above can of course also be used in combination with PutAsync instead of PostAsync. You may also want to use "text/xml" instead of "application/xml" depending on your needs.

That is it, I hope this post was of help to you.