0

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.

Skip
  • 95
  • 8
  • I've never heard of the `UPDATE` method of REST... Are you sure you don't mean to `PUT` or `PATCH`? – maccettura Dec 19 '22 at 14:54
  • The documentation is rather clear on that, yes. I asked the support and they answered to just set REQUEST_METHOD to UPDATE in header, but I don't find a way to do this in C#. – Skip Dec 19 '22 at 14:57
  • I tried it with Postman which offers UPDATE as a method and I receive 200. – Skip Dec 19 '22 at 14:58
  • 1
    Well in that case you can just use the [`HttpMethod` constructor](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpmethod?view=net-7.0) with `"UPDATE"` as a parameter: – maccettura Dec 19 '22 at 15:00
  • 1
    Exactly what I've been looking for. I wonder how I missed that constructor. Mind making this a reply I can accept? – Skip Dec 19 '22 at 15:33

1 Answers1

2

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");
maccettura
  • 10,514
  • 3
  • 28
  • 35