Post an empty body to REST API via HttpClient

Ryan Rinaldi picture Ryan Rinaldi · Oct 26, 2011 · Viewed 67.4k times · Source

The API I'm trying to call requires that I do a POST but with an empty body. I'm new to using the WCF Web API HttpClient and I can't seem to find out the write code that would do a post with an empty body. I find references to some HttpContent.CreateEmpty() method, but I don't think that is for the Web API HttpClient code since I can't seem to find that method.

Answer

Alexander Zeitler picture Alexander Zeitler · Oct 26, 2011

Use StringContent or ObjectContent which derive from HttpContent or you can use null as HttpContent:

var response = await client.PostAsync(requestUri, null);