0

I try to delete a configuration by calling an Delete API. For my API requests I use Restsharp because it works fine with GET and POST.

Now i have following problem. When I try to call delete I get Error

405 "MethodNotAllowed".

In my response I have following message:

"HTTP method not allowed, supported methods: GET"

Code snipet:

var client = new RestClient("http://test.de/");
var request = new RestRequest("testrules/test");
request.Credentials = new NetworkCredential("username", "password");
var response = client.Delete(request);

I also tried the client.Execute() method and adding the id of the element which I want to delete to set over AddParameter() and AddUrlSegment() methods.

I've tracked the response in Fiddler:

DELETE http://test.de/testrules/test HTTP/1.1 
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml 
User-Agent: RestSharp/105.0.1.0 
Host: test.de
Content-Length: 0
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

When i try my request with Advanced RestClient from Chrome i get following request in Fiddler:

DELETE http://test.de/testrules/test HTTP/1.1
Host: test.de
Connection: keep-alive
Authorization: Basic Q29udGludW91czp0ZXN0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

Does anybody know a example for Delete with RestSharp? I've looked at this example from stackoverflow: RestSharp simple complete example and on the examples from the RestSharp documentation but nothing of this examples helped me.

Community
  • 1
  • 1
Schweimi
  • 1
  • 2

1 Answers1

0

Do you have the Rest API service code that you can post? From what you wrote, it feels like the restsharp client issues the Delete request using HTTP DELETE keyword while the Rest API endpoint accepts HTTP GET for the Delete method; but without the info about the server-side code it's hard to tell.

ofcoursedude
  • 456
  • 4
  • 10
  • No i don't have the server-side code. But i know that the server only accept delete. Why should it work with Advanced RestClient then? There i configure delete and when i call the api with the restclient i have to use delete otherwise it wont work. I think there is a problem with RestSharp. Maybe i have something configured wrong but i am not really sure. – Schweimi Apr 02 '15 at 13:56
  • There is one more difference I can see in the two requests - that is, the Advanced RestClient's headers have Authorization line. Could that be the issue, that the DELETE method requires authorization while GET doesn't? – ofcoursedude Apr 02 '15 at 13:59
  • (sorry, it wasn't clear from the post that the Advanced RestClient request actually works) – ofcoursedude Apr 02 '15 at 14:01
  • I use for authorization the credentials of the client but it is not shown in Fiddler. I don't know why. But GET and POST are working with authorization and for this requests i can't see it too in Fiddler. Thats how i set the credentials just forgot to post it in the descreption. request.Credentials = new NetworkCredential("username", "password"); – Schweimi Apr 02 '15 at 14:27
  • I'm not familiar with RestSharp, so I'm not really sure. But from https://github.com/restsharp/RestSharp/wiki/Authenticators it seems like you also need to add an authenticator to the client, not just the request. – ofcoursedude Apr 02 '15 at 14:34
  • Please don't start a conversation with an answer. If you need clarification, then use a comment on the original question until you have enough information to post an answer. – Steve Mitcham Apr 02 '15 at 14:34
  • I've tried it with the SimpleAuthenticator class and there is no difference in the response. Still same result. – Schweimi Apr 02 '15 at 14:43
  • For debugging purposes, how about adding the authorization header manually? (I'm sure restsharp will let you manipulate the headers) – ofcoursedude Apr 02 '15 at 14:52