When to use RequestHandler.get_argument()
, RequestHandler.get_query_argument()
and RequestHandler.get_body_argument()
?
What is the use-case for each of them?
Also what does the request.body
and request.argument
do in these cases? Which are to be used in which scenarios?
And, is there a request.query
or something similar too?
Most HTTP requests store extra parameters (say, form values) in one of two places: the URL (in the form of a
?foo=bar&spam=eggs
query string), or in the request body (when using a POST request and either theapplication/x-www-form-urlencoded
ormultipart/form-data
mime type).The
Request.get_query_argument()
looks for URL parameters, theRequestHandler.get_body_argument()
lets you retrieve parameters set in the POST body. TheRequestHandler.get_argument()
method retrieves either a body or a URL parameter (in that order).You use
Request.get_argument()
when you explicitly don't care where the parameter comes from and your endpoint supports both GET and POST parameters. Otherwise, use one of the other methods, to keep it explicit where your parameters come from.The
Request.get_*_argument
methods use therequest.body_arguments
andrequest.query_arguments
values (withrequest.arguments
being their aggregate), decoded to Unicode.request.body
is the undecoded, unparsed raw request body; and yes, there is an equivalentself.query
containing the query string from the URL.