Python+requests编写接口用例
1、python编写接口用例需要依赖requests模块,我们先确认本地是否安装了requests模块,没有安装的先进行安装。
pycharm工具查看已安装的模块:File->Setting->Project Interpreter
2、python代码
import requests def get(url, header): getPath = '/sale/list' #params参数 get_params = {'Id': 8, 'No': 'B135740'} #发起get请求 getResult = requests.request('get', url + getPath, params=get_params, headers=header) print(getResult.text) def post(url, header): postPath = '/sale/transfer' #json参数 post_json ={"saleNo": "K2023030001","Details": [{"from": 1,"Id": 24,"to": 4}]} #发起post请求 postResult = requests.request('post', url + postPath, json=post_json, headers=header) print(postResult.text) re_headers={'Authorization':'Bearer eyJh...'} #re_headers = {'x-token': 'ad2...','Content-Type': 'application/json','x-client': '...'} URL = 'http://xxxx.com' get(URL, re_headers) post(URL, re_headers)
3、执行脚本成功,打印响应信息