Python: Bottle HTTP Request methods














































Python: Bottle HTTP Request methods



Description:

For different tasks, there are different kinds of HTTP request methods available. If no particular method is specified then GET is considered as the default method for all the routes. Thus all the routes will match the GET requests only. Other requests methods available are:

  1. GET

  2. PUT

  3. POST

  4. DELETE or PATCH


To handle these above-mentioned methods, an additional (method) keyword argument can be added to the route() decorator.

Alternatively, the following decorators can also be used to implement the above mentioned methods:

  1. get()

  2. put()

  3. post()

  4. delete() or patch()


Whenever any kind of online forms are submitted, they are usually submitted using the POST method as in this method, all the collected data is not sent/seen in the address bar and thus it ensures security.


The following example shows the use of the POST method while handling a login form.




CODE:







EXPLANATION:

In the above example, /login URL is connected to two different callbacks, one is for the GET requests while another is for the POST requests. The first request shows an HTML form to the user. The second callback is called on form submission and verifies the login credentials given by the user.





SPECIAL METHODS: HEAD & ANY

The HEAD method is similar to the GET methods in terms of asking for the response but it is without the response body. This is helpful to retrieve the meta-data about any resource without actually downloading the whole file. This is automatically handled by the Bottle and you don't need to specify any HEAD routes explicitly.

Routes that listen to ANY method matches requests regardless of their HTTP method but only if no other more specific route is declared. This is useful for proxy-routes that redirect requests to more specific sub-applications.



Comments