| 1 2 3 4 5 6 7 8 9 10 11 12 13 | def post(url, data=None, json=None, **kwargs):
r``""``"Sends a POST request.
:param url: URL``for
the``new
:``class``:
Requestobject``.` `:param data: (optional) Dictionary, list of tuples, bytes, or file-like` `object` `to send``in` `the body of the :``class``:`Request`.` `:param json: (optional) json data to send``in` `the body of the :``class``:`Request`.` `:param \*\*kwargs: Optional arguments that ``request`` takes.` `:``return``: :``class``:`Response <Response>
object
:rtype: requests.Response
“”"` `return` `request(
‘post’``, url, data=data, json=json, **kwargs)` |
说明:
从源码中注释看,告诉我们post请求报文中既可以传data,也可以传json。并且data与json,既可以是str类型,也可以是dict类型。
- json与data参数规则:
一、JSON
1.使用json参数,不管报文是str类型,还是dict类型,如果不指定headers中content-type的类型,默认是:application/json。
二、DATA
1.使用data参数,报文是dict类型,如果不指定headers中content-type的类型,默认application/x-www-form-urlencoded,相当于普通form表单提交的形式,会将表单内的数据转换成键值对,此时数据可以从request.POST里面获取,而request.body的内容则为a=1&b=2的这种键值对形式。
注意:即使指定content-type=application/json,request.body的值也是类似于a=1&b=2,所以并不能用json.loads(request.body.decode())得到想要的值。
2.使用data参数,报文是str类型,如果不指定headers中content-type的类型,默认application/json。
综上所述,两种参数的使用情况:
用data参数提交数据时,request.body
的内容则为a=1&b=2
的这种形式,用json参数提交数据时,request.body
的内容则为’{"a": 1, "b": 2}'
的这种形式
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036
实例解析
知道了上面所讲的理论,需要通过实例来验证一下。
以我django项目开发的web接口为例:
url.py文件
1 2 3 4 5 6 7 8 9 10 | from django.contrib import admin from django.urls import path,include from django_web.views import views from django.conf.urls import url urlpatterns = [ path(``'admin/'``, admin.site.urls), #首页 path(``'index/'``,views.test_dj), #登陆页面 |
view.py文件
1 2 3 4 5 6 7 8 9 | def test_dj(request): print(request.body,111) print(request.headers) ""``" 当post请求的请求体以data为参数,发送过来的数据格式为:b``'username=test&password=123' 当post请求的请求体以json为参数,发送过来的数据格式为:b``'{"username": "test", "password": "123"}' ""``" return render(request,``'index.html'``) |
exp1:
test.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import requests headers = {``'content-type'``:``'application/json'``} data = { "username"``:``"test"``, "password"``:``"123" } print(type(data)) #dict r1 = requests.post(url=``"http://127.0.0.1:8000/index/"``,data=data) print(r1.text) |
返回的报文:
1 2 | b``'username=test&password=123' 111 {``'Content-Length'``:``'26'``,``'Content-Type'``:``'application/x-www-form-urlencoded'``,``'Host'``:``'127.0.0.1:8000'``,``'User-Agent'``:``'python-requests/2.22.0'``,``'Accept-Encoding'``:``'gzip, deflate'``,``'Accept'``:``'*/*'``,``'Connection'``:``'keep-alive'``} |
**exp1案例:**证实了第二条规则:使用data参数,报文是dict类型,不指定content-type,默认是:application/x-www-form-urlencoded,请求数据格式是:key1=value1&key2=value2键值对形式。
exp2:
test.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import requests headers = {``'content-type'``:``'application/json'``} data = { "username"``:``"test"``, "password"``:``"123" } print(type(data)) #dict r1 = requests.post(url=``"http://127.0.0.1:8000/index/"``,json=data) print(r1.text) |
将参数data换成json请求,返回的报文:
1 2 | b``'{"username": "test", "password": "123"}' 111 {``'Content-Length'``:``'39'``,``'Content-Type'``:``'application/json'``,``'Host'``:``'127.0.0.1:8000'``,``'User-Agent'``:``'python-requests/2.22.0'``,``'Accept-Encoding'``:``'gzip, deflate'``,``'Accept'``:``'*/*'``,``'Connection'``:``'keep-alive'``} |
**exp2案例:**证实了第一条规则:使用json参数,报文是dict类型,如果不指定headers中content-type的类型,默认是:application/json,请求数据格式是:dict形式。
exp3:
test.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import requests,json headers = {``'content-type'``:``'application/json'``} data = { "username"``:``"test"``, "password"``:``"123" } print(type(data)) #dict # 方法1 r1 = requests.post(url=``"http://127.0.0.1:8000/index/"``,data=json.dumps(data)) # 方法2 r2 = requests.post(url=``"http://127.0.0.1:8000/index/"``,json=json.dumps(data)) print(r1.text) print(r2.request) |
查看返回报文:
1 2 3 4 5 | b``'{"username": "test", "password": "123"}' 111 {``'Content-Length'``:``'39'``,``'Content-Type'``:``'text/plain'``,``'Host'``:``'127.0.0.1:8000'``,``'User-Agent'``:``'python-requests/2.22.0'``,``'Accept-Encoding'``:``'gzip, deflate'``,``'Accept'``:``'*/*'``,``'Connection'``:``'keep-alive'``} b``'"{\\"username\\": \\"test\\", \\"password\\": \\"123\\"}"' 111 {``'Content-Length'``:``'49'``,``'Content-Type'``:``'application/json'``,``'Host'``:``'127.0.0.1:8000'``,``'User-Agent'``:``'python-requests/2.22.0'``,``'Accept-Encoding'``:``'gzip, deflate'``,``'Accept'``:``'*/*'``,``'Connection'``:``'keep-alive'``} [01/Mar/2020 14:02:11]``"POST /index/ HTTP/1.1" 200 1890 |