Tornado - What is the difference between RequestHandler get_argument (), get_query_argument () and get_body_argument ()?

When to use RequestHandler.get_argument() , RequestHandler.get_query_argument() and RequestHandler.get_body_argument() ?

What is a use case for each of them?

And what do request.body and request.argument in these cases? Which should be used in what scenarios?

And is there a request.query or something similar?

+5
source share
1 answer

Most HTTP requests store additional parameters (for example, form values) in one of two places: the URL (as a query string ?foo=bar&spam=eggs ), or in the request body (when using a POST request and type mime application/x-www-form-urlencoded or multipart/form-data ).

Request.get_query_argument() looks for URL parameters, RequestHandler.get_body_argument() allows you to retrieve the parameters set in the POST body. The RequestHandler.get_argument() method retrieves either the body or the URL parameter (in that order).

You use Request.get_argument() when it is clearly not important for you where this parameter comes from, and your endpoint supports both GET and POST parameters. Otherwise, use one of the other methods to save it explicitly where your options come from.

The methods Request.get_*_argument use the values request.body_arguments and request.query_arguments (in this case request.arguments is their combination), it is decoded in Unicode. request.body - unprocessed unprocessed request object; and yes, there is a self.query equivalent containing a query string from a URL.

+13
source

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


All Articles