接口自动化知识碎片

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}}',
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值