How do I send an OPTIONS request with System.Net.Http.HttpClient

exposed methods for HttpClient

  • DeleteAsync
  • GetAsync
  • PostAsync
  • PutAsync
  • few others as well.....

I was expecting a OptionsAsync

   switch (httpMethod) {
      case HTTP_METHODS.DELETE:
       {
        httpResponseMessage = httpClient.DeleteAsync(uri).Result;
        break;
       }
      case HTTP_METHODS.GET:
       {
        httpResponseMessage = httpClient.GetAsync(uri).Result;
        break;
       }
      case HTTP_METHODS.POST:
       {
        httpResponseMessage = httpClient.PostAsync(uri, httpContent).Result;
        break;
       }
      case HTTP_METHODS.PUT:
       {
        httpResponseMessage = httpClient.PutAsync(uri, httpContent).Result;
        break;
       }
      case HTTP_METHODS.OPTION:
       {
        //not sure what method to call on httpclient here to make Options request
        httpResponseMessage = httpClient.PutAsync(uri, httpContent).Result;
        if (httpResponseMessage.Headers.Contains("X-CSRF-TOKEN")) {
         IEnumerable < string > headerValues = httpResponseMessage.Headers.GetValues("X-CSRF-TOKEN");
         csrfToken = headerValues.FirstOrDefault();
        }
        break;
       } 
     }

1 Answers

2
Haytam On Best Solutions

There are no wrappers for that kind of methods (e.g. OPTIONS and HEAD), but you could use SendAsync just like these wrappers do:

var request = new HttpRequestMessage(HttpMethod.Options, "url");
var result = await _httpClient.SendAsync(request);