python实现http接口测试
get请求
import requests
url="http://apis.juhe.cn/cook/query?key=ccb4d2dd3c1f0feb4788da1920d7207c&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=3"
reponse=requests.get(url)
print(reponse.text)
post请求
a.以表单形式提交请求数据
url="http://117.122.238.33/support/login"
data1={'username':"John", "password":"green"}
response=requests.post(url,data=data1)
print(response.status_code)
print(response.text)
b.以json的格式提交请求数据
url="http://117.122.238.33/webservice/services/Rest/account"
#请求头添加需要请求的格式
custom_headers = {'Content-Type': 'application/json'}
payload = {
"name": "jiaminqiang",
"billingAddress": "beijing",
"phoneNumber": "15801396646"
}
r = requests.post(url, json=payload, headers=custom_headers)
print(r.text)
c.以xml的格式提交请求数据
import requests
url = "http://117.122.238.33/webservice/services/Rest/account"
payload = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account>
<name>jiaminqiang2</name>
<billingAddress>beijing</billingAddress>
<phoneNumber>15901396645</phoneNumber>
</account>"""
headers = {
'content-type': "application/xml",
}
response = requests.post(url, data=payload, headers=headers)
print(response.text)