代码实现接口测试——requests

1)获取URL参数

#导包
import requests
#发送请求 1)通过URL传递参数
response = requests.get("https://api.uomg.com/api/comments.163?format=text")

#发送请求 2)通过params传递参数 
#   1、字符串 
response = requests.get(url="https://api.uomg.com/api/comments.163",params="format=text")
urlnew="https://api.uomg.com/api/comments.163"
paramsnew = "format=text"
response = requests.get(url=urlnew,params=paramsnew)

#   2、字典
urlnew="https://api.uomg.com/api/comments.163"
dictionary = {
    "format":"text"
}
response = requests.get(urlnew,dictionary)

#查看响应
print(response.text)

 2)发送form表单和json数据

# 1.导入requests库
import requests

# 2.明确请求地址
url = "http://httpbin.org/post"
# 3.明确请求参数
data = {
    "dep_id": "cscscs",
    "dep_name": "cscscscs",
    "master_name": "cscscscs",
    "slogan": "cscscscscs"
}
# 4.发送请求
response = requests.post(url=url, data=data)

print(response.json())

3)获取状态码、URL、响应头、cookie、文本形式、字节形式

# 1.导入requests库
import requests

# 2.明确请求地址
response = requests.get("http://www.baidu.com")
# 3.获取响应状态码
print("状态码为:",response.status_code)
# 4.获取请求URL
print("URL:",response.url)
# 5.获取响应头数据
print("响应头数据:",response.headers)
print("Content-Type:",response.headers.get("Content-Type"))
# 6.获取响应cookie数据
print("cookie响应:",response.cookies)
print("BDORZ:",response.cookies.get("BDORZ"))
# 6.获取文本形式响应内容
response.encoding = "utf-8"
print("文本形式:",response.text)
# 6.获取字节形式响应内容
print("字节形式:",response.content)
#解码
print("字节形式解码:",response.content.decode("utf-8"))

#tip!!!!只有json返回的数据用json格式,其他格式会有错误
print(response.json())

4)设置请求头header

#1、导包
import requests
#2、发送请求
newurl = "https://api.uomg.com/api/comments.163"
newheader = {
    "Content-type":"application/json"
}
newdata = {
    "dep_id": "cscscs",
    "dep_name": "cscscscs",
    "master_name": "cscscscs",
    "slogan": "cscscscscs"
}
response = requests.post(newurl,newdata,newheader)
#3、查看响应
print(response.json())

 5)cookie的登录(验证码)和查询

#1、导包
import requests
#2、获取cookie
#获取cookie值并输出
response = requests.get("~~~~~~~~~~~~~获取cookie~~~~~~~~~")
print(response.cookies)
cookie = response.cookies.get("cookie_tp")
print(cookie)


#3、登录
#设置newcookie并发送请求
newurl = "~~~~~~~~~~~~登录地址~~~~~~~~~~~~"
newdata = {
    "name": "zhaoge",
    "pas": "123456",
    "code":"1111"
}
newcookie = {
    "cookie_tp":cookie
}
response = requests.post(newurl,newdata,newcookie)
print(response.json())

#4、查询订单
#其他操作都要带上newcookie
response = requests.get("~~~~~查询地址~~~~~~",newcookie)
print(response.text)

  6)session的登录(验证码)和查询

#1、导包
import requests

#创建session
session = requests.session()

#2、获取cookie
response = session.get("~~~~~~~~~~~~~获取cookie~~~~~~~~~")

#3、登录
newurl = "~~~~~~~~~~~~登录地址~~~~~~~~~~~~"
newdata = {
    "name": "zhaoge",
    "pas": "123456",
    "code":"1111"
}
response = session.post(newurl,newdata)
print(response.json())

#4、查询订单
response = session.get("~~~~~查询地址~~~~~~")
print(response.text)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值