0

In webapp2, I've not been able to make an HTTP DELETE request work correctly. As a workaround, I'm using a different URI with GET, but I'd prefer a more RESTful approach.

With this code, the server log shows the DELETE request as a GET request. What am I missing here?

class TeamPages(webapp2.RequestHandler):
    def get(self, team_name):
        ...

    def post(self, team_name):
        ...

    def delete(self, team_name):
        key_name = team_name.upper()
        Team.delete(key_name)
        self.redirect('/teams')

A GET request to /teams/{{ team_name }} responds with a page that includes the following html, but when I submit, it requests the GET method instead of the DELETE method.

<form action="/teams{{ team.team_name }}" method="delete">
    <button type="submit">Delete</button>
</form>

Update

Additional info... I'm developing under Google App Engine and I'm using Chrome on a Mac. Here is the request header that shows GET instead of DELETE...

GET /teams/hornets? HTTP/1.1
Host: localhost:9080
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: dev_appserver_login="test@example.com:True:185804764220139124118"
Referer: http://localhost:9080/teams/hornets
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36

And here is the response header...

HTTP/1.1 405 Method Not Allowed
allow: DELETE
Cache-Control: no-cache
Content-Length: 187
content-type: text/html; charset=UTF-8
Date: Tue, 10 Jun 2014 21:24:36 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Server: Development/2.0
bholben
  • 1,077
  • 1
  • 15
  • 24
  • Are you sure it doesn't ever enter the delete function? What happens if you remove the redirect? – Tim Jun 10 '14 at 20:42
  • It never enters the delete method. Regardless of the redirect, I get a 405 exception: _The method GET is not allowed for this resource._ – bholben Jun 10 '14 at 21:20
  • Is there maybe a missing `/` in the form action url? – Tim Jun 10 '14 at 21:21
  • The url works perfectly when I swap out the DELETE verbs with GET verbs on both sides, so I don't think there is a missing slash. I've edited my post to add header details. Perhaps the request header will expose some clues? – bholben Jun 10 '14 at 21:38
  • 1
    The problem is with your form, not the server. method=delete isn't valid - submitting the form is sending a GET. If you want a plain form, then POST is a safer choice. – Greg Jun 10 '14 at 21:42
  • What is the proper way to use DELETE in a form? – bholben Jun 10 '14 at 21:45
  • From [this](http://stackoverflow.com/q/5162960/3465372) post, it looks like HTML is not the way to go for PUT and DELETE. I don't know AJAX yet, so I'll probably just stick with a different URI and GET. – bholben Jun 10 '14 at 21:53
  • If you trying to stay close to REST, You should at least use a POST rather than a GET. POST do have side effects, where as GET should really on get stuff. – Tim Hoffman Jun 10 '14 at 23:15
  • These URLs require user authentication. My latest thinking on using GET (instead of POST) is to allow a registered user to bypass the GUI and simply type in a URL to effect a delete operation. For example, /teams/hornets/delete would delete hornets. Is this a bad idea? This is my first app, so I'm still a long way from calling myself experienced. – bholben Jun 11 '14 at 00:03

1 Answers1

1

Short answer: The issue is not with the web framework or the browser. This is an HTML limitation. HTML only allows for GET and POST. No other methods are permitted. I've adjusted my title to reflect the subject matter more accurately.

More details:

Based on the HTML clue from @Greg, I searched some more. In the comments of a related post, @Guandalino added some great links:

*Bug report 16071

*Stack Exchange

This helps to add some color and arguments for and against the addition of PUT and DELETE to the HTML specification.

To me (and I'm a newbie), this incompatibility with HTTP does not seem to be congruent with REST principles. I'd think that the mere recognition that so many web frameworks have created workarounds is justification enough for making this standard in a future HTML version.

Thanks to everyone for the feedback!

Community
  • 1
  • 1
bholben
  • 1,077
  • 1
  • 15
  • 24
  • A great package that enables HTTP DELETE in HTML is htmx. It is a javascript library that extends HTML and fills in gaps like not being able to send a DELETE request. I have been using it for personal projects and I would recommend it: https://htmx.org/ – Zach Bellay Dec 13 '21 at 21:02