10

I'm trying to set custom headers on a HttpClient.DeleteAsync request. I've tried using

httpClient.DefaultRequestHeaders.Add("X-Parse-Application-Id",ParseAppID);

but get this error

Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

HttpClient.SendAsync can send custom request headers with

System.Net.Http.HttpRequestMessage.Headers.Add("X-Parse-Application-Id",ParseAppID);

and HttpClient.PostAsync can send them with

System.Net.Http.StringContent.Headers.Add("X-Parse-Application-Id",ParseAppID);

How can I do this with DeleteAsync?

1
  • I'm not able to reproduce this. The code here works without issue for me. Did you get to the bottom of it? Commented Jun 22, 2015 at 20:05

1 Answer 1

4

Instantiate an HttpRequestMessage and use SendAsync instead:

var request = new HttpRequestMessage(HttpMethod.Delete, requestUri);
request.Headers.Add("X-Parse-Application-Id", ParseAppID);

using (var response = await _calendarClient.SendAsync(request).ConfigureAwait(false))
{
    if (response.StatusCode.Equals(HttpStatusCode.NotFound))
    {
        return;
    }
    response.EnsureSuccessStatusCode();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Such a bummer that I cannot do that with httpclient()
calendarClient is an HttpClient.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.