Home > Software engineering >  Can you use custom HTTP request methods with http.client?
Can you use custom HTTP request methods with http.client?

Time:12-20

Is there a way to use HTTP request methods that are not implemented by System.Net.Http.HttpMethod?

I try to update files with a REST interface. The way it is implemented, I GET a list of files and their hashes. Then I check if any of these files have changed on my side and if so, I POST each file to the API, otherwise I skip it.

When I'm done, the endpoint expects an UPDATE request to know that I'm done sending files. But there is no UPDATE method in HttpMethod.

Is there a way to alter REQUEST_METHOD manually in a HttpRequestMessage or do they need to recode the endpoint?

Looking up System.Net.Http.HttpMethod only gives the following options: GET, PUT, POST, DELETE, HEAD, OPTIONS, TRACE, PATCH and CONNECT. There is no obvious way to add a custom method.

CodePudding user response:

In the case where you need an HttpMethod that does not exist in the static properties of the class, you can just use the constructor which allows you to pass any string method:

var customHttpMethod = new HttpMethod("UPDATE");
  • Related