python接口测试(requests库)-1

python接口测试(requests库)-1
requests是python的http库
参考:https://blog.csdn.net/qq_31524409/article/details/80984729
https://www.cnblogs.com/qiaoxin/articles/7928290.html

接口测试的步骤:
1>获取接口的url地址
2>查看接口的发送方式
3>添加请求头、请求体
4>查看返回结果,校验返回结果是否正确

#urllib的get请求

import urllib.request

url=‘http://www.baidu.com

response=urllib.request.Request(url=url)

html=urllib.request.urlopen(response)

print(html.getcode())

print(html.headers)

#urllib的post代码
import urllib.request

import urllib.parse

url=‘http://www.tuling123.com/openapi/api

data={“key”: “your”, “info”: ‘你好’}

data=urllib.parse.urlencode(data).encode(‘utf-8’)

re=urllib.request.Request(url,data)

html=urllib.request.urlopen(re)

print(html.getcode(),html.msg)

print(html.read())

#request库的get请求
import requests

r = requests.get(‘https://www.baidu.com’)

print(r.headers)

#requests库的post请求
import requests
payload = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
r = requests.post(“http://httpbin.org/post”, data=payload)
print(r.text)

猜你喜欢

转载自blog.csdn.net/cqupt_zl/article/details/83350988