1、接口请求
get query:path\query
post body:
form:
结构化请求:json \ xml \ json rpc
binary
1、文件上传
files={'file':open('report.xls','rb')}
r=requests.post(url,files=files)
2、header
headers = {'user-agent':'my-app/0.0.1'}
r = requests.get(url,headers=headers)
3、cookie
cookies = dict(cookies_are='working')
r = requests.get(url,cookies=cookies)
4、相应结果
r.url\ r.status_code\ r.headers\ r.cookies
r.text = r.encoding + r.content
r.json() = r.encoding + r.content + content type json
r.raw.read(10)
对应的请求内容:r.request
5、例子:
import requests
import pytest
class TestDemo:
def test_headers(self):
r=requests.get('https://httpbin.testing-studio.com/get',headers={"H":"header demo"})
print(r.status_code)
print(r.text)
print(r.json)
assert r.status_code == 200
assert r.json()['headers']["H"] == "header demo"
6、结构化请求体构造
1)json请求体构
json = payload自动转换为json的形式传递,data = payload以form表单的形式进行传递
payload = {'some':'data'}
r = requests.post(url,json=payload)
举例
def test_post_json(self):
payload = {
"level":1,
"name":"jack"
}
r = requests.post("https://httpbin.testing-studio.com/post",json=payload)
print(r.text)
assert r.status_code == 200
assert r.json()['json']['level'] == 1
2)xml 请求
xml = """<?xml version='1.0' encoding='utf-8'?>
<a>6</a>
"""
headers = {'Content-Type':'application/xml'}
r = requests.post('https://httpbin.org/post',data=xml,headers=headers).text
3)复杂数据解析
数据保存:将复杂的xml或者json请求踢保存到文件模版中
数据处理:
使用mustache、freemaker等工具解析
简单的字符串替换
使用json xml api进行结构化解析
数据生成:输出最终结果
import pystache
pystache.render(
'Hi{
{person}}',