I came across an interesting issue recently. I had a form with an action set to “GET”. The corresponding view was a django-rest-framework view that was just supposed to allow for GET requests. However, I couldn’t get it to work because drf was expecting lower case names, and it turns out that HTML5 expects a lowercase action set to “get”.
Yet the request variables are capitalized.
request.GET
request.POST
I suppose that we’re saying that those variables are “constants” and should follow the naming convention of capitalizing them. They are immutable dictionaries after all. So I guess I’ve answered my own question.
On donderdag 10 mei 2018 15:39:21 CEST Matthew Pava wrote:
> Your reference is interesting because it seems to imply that the method
> attribute has to be uppercase. Maybe the browser translates it to
> uppercase before sending it to the server?
Again, HTML and requests are not strictly related. Requests are made using HTTP and yes, the browser is responsible for making that request. HTML has four ways of instructing the browser to make a request:
- A <form> submission
- <a> Hyperlink
- A src property of a resource reference (through img / script / link etc tags)
- A <meta> refresh tag
And yes, HTTP request methods are uppercase:
% telnet localhost 80
....
Escape character is '^]'.
get / http/1.1
HTTP/1.1 400 Bad Request
Now with capitals:
Escape character is '^]'.
GET / HTTP/1.1
Host: localhost
HTTP/1.1 301 Moved Permanently
> But then that doesn't explain
> why I got two different results based on the case of the method attribute.
Lowercase `get` is a common method, for one on all dictionary-like objects in Python. But the get method isn't implemented on standard Django Request objects. So even reading your original message, I couldn't tell you what went wrong. You probably analyzed the bug wrong as there are a few close but not quite the same things in the whole stack:
- HTML Form's method attribute
- A view that has a get() method that handles HTTP GET requests
- Django's HttpRequest object
- It's GET / POST dictionaries
- Which have a get() method
I've never come accross a browser that accepted only lowercase HTML attributes, although it may have been possible in the dark ages of XHTML 1.1 strict mode. I'd be thoroughly surprised if the only changing the case of the form's method attribute from `get` to `GET` would change anything.
--
Melvyn Sopacua