栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

接口测试-python,通过requests发送请求,带表单和json参数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

接口测试-python,通过requests发送请求,带表单和json参数

一、示例:向百度发送get请求(前提已经安装了requests包):

import requests

baiduRespones = requests.get("http://www.baidu.com")

print(baiduRespones.text)

注意:响应结果中文会出现乱码的处理方法,修改编码格式,例如:

baiduRespones.encoding = "utf-8"

二、get请求中添加参数

# (1)传递字符串
baiduRespones = requests.get("http://www.baidu.com?name=testname")
baiduRespones1 = requests.get(url="http://www.baidu.com", params="name=testname")
# (2)传递字典
dict = {
    "name": "testname"
}
baiduRespones2 = requests.get(url="http://www.baidu.com", params=dict)

 三、post请求中携带参数(参数为表单数据和json数据)

# 导入requests包
import requests

# 发送post请求
dataValue = {
        "head": {
            "code": 200,
            "message": ""
        },
        "body": {
            "parameters": {
                "username": "test",
                "password": "123456",
                "checkVerificate": "false",
                "verificateCacheKey": "",
                "verificate": ""
            },
            "datastores": {}
        }
}
postResponse = requests.post("http://192.168.1.79:8088/checkLogin", data=dataValue)
print(postResponse.json)

如果传递的参数是json数据,那么应该写成:

postResponse = requests.post("http://192.168.1.79:8088/checkLogin", json=dataValue)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号