Equivalent to WebRequest `curl -X DELETE ...`

I'm having trouble creating the WebRequest equivalent of the following curl command:

curl -vX DELETE "http://admin: 123@localhost :5984/booster" 

The command works fine, gives the following output:

 C:\Projects\Booster\Bin>curl -vX DELETE "http://admin: 123@localhost :5984/booster" * About to connect() to localhost port 5984 (#0) * Trying 127.0.0.1... connected * Server auth using Basic with user 'admin' > DELETE /booster HTTP/1.1 > Authorization: Basic YWRtaW46MTIz > User-Agent: curl/7.22.0 (i386-pc-win32) libcurl/7.22.0 OpenSSL/0.9.8r zlib/1.2.5 > Host: localhost:5984 > Accept: */* > < HTTP/1.1 200 OK < Server: CouchDB/1.0.2 (Erlang OTP/R14B) < Date: Fri, 25 Nov 2011 01:03:45 GMT < Content-Type: text/plain;charset=utf-8 < Content-Length: 12 < Cache-Control: must-revalidate < {"ok":true} * Connection #0 to host localhost left intact * Closing connection #0 

and deletes the CouchDB database as expected, but when I use the following equivalent code:

 try { var request = WebRequest.Create("http://admin: 123@localhost :5984/booster"); request.Headers.Clear(); request.Method = "DELETE"; var response = request.GetResponse(); Console.WriteLine(response.GetResponseString()); } catch (WebException e) { Console.WriteLine(e.Response.GetResponseString()); } 

To do the same thing as the curl command, I get an exception message

"The remote server returned an error: (405) Method not allowed."

The server responds with '{"error": "method_not_allowed", "reason": "Only GET, HEAD allowed"}'

Question:

What is the difference between a curl command and a command executed via WebRequest? Why does the first case work, but the second does not work?

+4
source share
1 answer

I managed to find out what the problem is, so here:

Wireshark showed the following message between client and server:

WebRequest Case

 DELETE /booster HTTP/1.1 Host: localhost:5984 Connection: Keep-Alive HTTP/1.1 302 Moved Temporarily Server: CouchDB/1.0.2 (Erlang OTP/R14B) Location: http://localhost:5984/_utils/session.html?return=%2Fbooster&reason=You%20are%20not%20a%20server%20admin. Date: Fri, 25 Nov 2011 09:54:29 GMT Content-Type: text/plain;charset=utf-8 Content-Length: 64 Cache-Control: must-revalidate {"error":"unauthorized","reason":"You are not a server admin."} DELETE /_utils/session.html?return=%2Fbooster&reason=You%20are%20not%20a%20server%20admin. HTTP/1.1 Host: localhost:5984 HTTP/1.1 405 Method Not Allowed Server: CouchDB/1.0.2 (Erlang OTP/R14B) Date: Fri, 25 Nov 2011 09:54:29 GMT Content-Type: text/plain;charset=utf-8 Content-Length: 64 Cache-Control: must-revalidate Allow: GET,HEAD {"error":"method_not_allowed","reason":"Only GET,HEAD allowed"} 

Swirl Case

 DELETE /booster HTTP/1.1 Authorization: Basic YWRtaW46MTIz User-Agent: curl/7.22.0 (i386-pc-win32) libcurl/7.22.0 OpenSSL/0.9.8r zlib/1.2.5 Host: localhost:5984 Accept: */* HTTP/1.1 404 Object Not Found Server: CouchDB/1.0.2 (Erlang OTP/R14B) Date: Fri, 25 Nov 2011 09:54:14 GMT Content-Type: text/plain;charset=utf-8 Content-Length: 41 Cache-Control: must-revalidate {"error":"not_found","reason":"missing"} 

This shows that WebRequest does not use basic authorization, but curl uses it. A small number of search queries showed how to use basic authorization in WebRequest, and looks like this:

 try { var request = WebRequest.Create("http://localhost:5984/booster"); request.Headers.Clear(); request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("admin:123")); request.Method = "DELETE"; var response = request.GetResponse(); Console.WriteLine(response.GetResponseString()); } catch (WebException e) { Console.WriteLine(e.Response.GetResponseString()); } 
+3
source

Source: https://habr.com/ru/post/1382983/


All Articles