问题描述
何时使用 RequestHandler.get_argument()
, RequestHandler.get_query_argument()
和 RequestHandler.get_body_argument()
?
When to use RequestHandler.get_argument()
, RequestHandler.get_query_argument()
and RequestHandler.get_body_argument()
?
什么 用例 是针对每个人吗?
What is the use-case for each of them?
请求又是什么? .body
和 request.argument
在这些情况下做什么?在哪些场景中使用哪些?
Also what does the request.body
and request.argument
do in these cases? Which are to be used in which scenarios?
并且,是否有 request.query
或类似的东西?
And, is there a request.query
or something similar too?
推荐答案
大多数HTTP请求在两个地方之一存储额外的参数(比如表单值):URL(以表格形式) ),或在请求正文中(使用)。
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 the application/x-www-form-urlencoded
or multipart/form-data
mime type).
Request.get_query_argument()
查找URL参数, RequestHandler.get_body_argument()
可让您检索参数在POST正文中设置。 RequestHandler.get_argument()
方法检索正文或URL参数(按此顺序)。
The Request.get_query_argument()
looks for URL parameters, the RequestHandler.get_body_argument()
lets you retrieve parameters set in the POST body. The RequestHandler.get_argument()
method retrieves either a body or a URL parameter (in that order).
你当您明确不关心参数来自何处并且您的端点支持GET和POST参数时,请使用 Request.get_argument()
。否则,使用其他方法之一,在参数来源的地方明确说明。
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.
Request.get _ * _ argument
方法使用 request.body_arguments
和 request.query_arguments
值(带请求。参数
作为它们的聚合),解码为Unicode。 request.body
是未解码的,未解析的原始请求正文;是的,有一个等价的 self.query
包含来自URL的查询字符串。
The Request.get_*_argument
methods use the request.body_arguments
and request.query_arguments
values (with request.arguments
being their aggregate), decoded to Unicode. request.body
is the undecoded, unparsed raw request body; and yes, there is an equivalent self.query
containing the query string from the URL.
这篇关于龙卷风 - RequestHandler的get_argument(),get_query_argument()和get_body_argument()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!