MarkF
MarkF

Reputation: 962

How To Set Headers For DeleteAsync

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?

Upvotes: 10

Views: 2499

Answers (1)

rianjs
rianjs

Reputation: 7944

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();
}

Upvotes: 4

Related Questions