Python接口测试 requests

| 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类型。

  1. 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 10from 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 9def 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 15import 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 2b``'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 15import 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 2b``'{"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 20import 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 5b``'{"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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值