Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make an OPTIONS request with HttpClient

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;
       } 
     }
like image 383
owen gerig Avatar asked Apr 19 '19 20:04

owen gerig


People also ask

What is httpclient in HTTP protocol?

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web. HttpClient is a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.

What is httpclient in Laravel?

HttpClient is a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. HTTP response status codes indicate whether a specific HTTP request has been successfully completed.

What type of HTTP does Java HTTP client support?

The Java HTTP Client supports both HTTP/1.1 and HTTP/2. By default the client will send requests using HTTP/2. Requests sent to servers that do not yet support HTTP/2 will automatically be downgraded to HTTP/1.1. There are two ways to create an HttpClient.

Why can't I use httpcontent with the deleteasync method?

Because HTTP DELETE requests typically contain no body, the DeleteAsync method doesn't provide an overload that accepts an instance of HttpContent. To learn more about using different HTTP verbs with HttpClient, see HttpClient. HttpClient has the concept of delegating handlers that can be linked together for outgoing HTTP requests.


1 Answers

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);
like image 73
Haytam Avatar answered Oct 24 '22 15:10

Haytam