Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 765
MarkF Avatar asked May 15 '15 00:05

MarkF


1 Answers

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();
}
like image 54
rianjs Avatar answered Sep 20 '22 07:09

rianjs