Ryan Rinaldi
Ryan Rinaldi

Reputation: 4239

Post an empty body to REST API via HttpClient

The API I'm trying to call requires a POST with an empty body. I'm using the WCF Web API HttpClient, and I can't find the right code that will post with an empty body. I found references to some HttpContent.CreateEmpty() method, but I don't think it’s for the Web API HttpClient code since I can't seem to find that method.

Upvotes: 224

Views: 154165

Answers (8)

Kaveh Naseri
Kaveh Naseri

Reputation: 1266

You can send an empty object too:

var emptyObject = new object();
HttpResponseMessage httpResponse = await server.CreateClient().PutAsJsonAsync(Put.SomeUrl(), emptyObject);

Upvotes: 0

Morten Nørgaard
Morten Nørgaard

Reputation: 2809

If you wish to avoid 'null' and make your intentions clear, as do I, you can override the StringContent class:

    private sealed class DeliberatelyEmptyContent : StringContent
    {
        public DeliberatelyEmptyContent() : base(string.Empty)
        {
        }
    }

    public async Task ActivateUser(string xflowUserId)
    {
        var httpclient = _httpClientFactory.CreateClient(_namedHttpClientName);
        string activateUrl = $"/User/{xflowUserId}/Activate";

        _ = await httpclient.PutAsync(activateUrl, new DeliberatelyEmptyContent());
    }

Upvotes: 2

Mikheil Zhghenti
Mikheil Zhghenti

Reputation: 728

In case you do not want to pass null value you can you the following:

Task<HttpResponseMessage> task = httpClient.PostAsync(uri, new StringContent(String.Empty));

But, beside this, as above already discussed you could pass null in there as a parameter.

Upvotes: 2

Alexander Zeitler
Alexander Zeitler

Reputation: 13089

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

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

Upvotes: 283

abolfazl mousavi
abolfazl mousavi

Reputation: 89

To solve this problem, use this example:

   using (var client = new HttpClient())
            {
                var stringContent = new StringContent(string.Empty);
                stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
                var response = client.PostAsync(url, stringContent).Result;
                var result = response.Content.ReadAsAsync<model>().Result;
            }

Upvotes: 1

Ryan Tuck
Ryan Tuck

Reputation: 131

Have found that:

Task<HttpResponseMessage> task = client.PostAsync(url, null);

Adds null to the request body, which failed on WSO2. Replaced with:

Task<HttpResponseMessage> task = client.PostAsync(url, new {});

And worked.

Upvotes: 12

Ogglas
Ogglas

Reputation: 69928

Did this before, just keep it simple:

Task<HttpResponseMessage> task = client.PostAsync(url, null);

Upvotes: 136

Ivan G.
Ivan G.

Reputation: 5215

I think it does that automagically if your web method has no parameters or they all fit into URL template.

For example this declaration sends empty body:

  [OperationContract]
  [WebGet(UriTemplate = "mykewlservice/{emailAddress}",
     RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
     BodyStyle = WebMessageBodyStyle.Wrapped)]
  void GetStatus(string emailAddress, out long statusMask);

Upvotes: -6

Related Questions