flask.Request

本文深入解析Flask框架中Request对象的使用,包括其属性如environ、path、full_path等,以及方法如args和values的返回值示例。通过实例展示如何获取请求参数和请求数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

flask.Request

 

doc:http://flask.pocoo.org/docs/1.0/api/#incoming-request-data

 

1.      flask.Request

flask中Request类的一些属性和方法。

 

class flask.Request(environ, populate_request=True, shallow=False)

The request object used by default in Flask. Remembers the matched endpoint and view arguments.

It is what ends up as request. If you want to replace the request object used you can subclass this and set request_class to your subclass.

The request object is a Request subclass and provides all of the attributes Werkzeug defines plus a few Flask specific ones.

1.1.    属性

 

environ:The underlying WSGI environment.

path

full_path

script_root

url

base_url

url_root

提供了一些资源定位说明,也就是 IRI.

Imagine your application is listening on the following application root:

http://www.example.com/myapplication

And a user requests the following URI:

http://www.example.com/myapplication/%CF%80/page.html?x=y

In this case the values of the above mentioned attributes would be the following:

path

u'/π/page.html'

full_path

u'/π/page.html?x=y'

script_root

u'/myapplication'

base_url

u'http://www.example.com/myapplication/π/page.html'

url

u'http://www.example.com/myapplication/π/page.html?x=y'

url_root

u'http://www.example.com/myapplication/'

 

3、args和values:args返回请求中的参数,values返回请求中的参数和form

return json.dumps(request.args)        #url:http://192.168.1.183:5000/login?a=1&b=2、返回值:{"a": "1", "b": "2"}
return str(request.values)        #CombinedMultiDict([ImmutableMultiDict([('a', '1'), ('b', '2')]), ImmutableMultiDict([('username', '123'), ('password', '1234')])])

 

authorization

The Authorization object in parsed form.

 

content_length:正文大小,单位字节

The Content-Length entity-header field indicates the size of the entity-body in bytes or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

 

cookies:略

 

data:请求数据

file:上传的文件

 

 

headers:默认的值由WSGI提供。

The headers from the WSGI environ as immutable EnvironHeaders.

返回列表

 

method:请求方法,例如get/post

 

url:资源地址

 

form:返回form的内容。

return json.dumps(request.form) #{"username": "123", "password": "1234"}

 

转载于:https://www.cnblogs.com/wodeboke-y/p/11113966.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值