python接口自动化—request模块

01 request模块基本操作

# 导入requests模块
import requests

# ===准备请求相关数据===

url = "https://api.tuhu.cn/Product/GetTireList"

headers = {
    "content-type": "application/json"
}

data = {"orderType": 0, "vehicleId": "VE-GM-S07BT", "onlyOe": False, "province": "上海市", "provinceId": "", "city": "上海市",
        "cityId": "", "pageIndex": 1,
        "filter": {"BrandName": "", "TireSize": "195/55R15", "TireTab": "", "TireSeason": "", "MaxPrice": "",
                   "MinPrice": "", "TireRof": "防爆,非防爆", "TireSpeedRating": "", "TirePattern": "",
                   "SpecialTireSize": ""}}

# ===发送请求===
response = requests.post(url=url, json=data, headers=headers)

发送请求

------POST--------

1、参数类型为:"Content-Type": "application/json",发送请求的时候,使用json来传递参数
response = requests.post(url=url,json=data)
2、参数类型为:Content-Type": "application/x-www-form-urlencoded,使用data来进行传递
response = requests.post(url=url,data=data)

3、文件上传接口的参数类型为:"Content-Type": "multipart/form-data;文件应该使用files这个参数来进行传递

 file = {"pic": ("bj01.png", open(r"C:\Users\MuSen\Desktop\page\image\ico.png", "rb"), "image/png")}

response = requests.post(url=url, files=file,data=data)

------GET-------

1、处理方式一:放到url地址后面
url = "http://httpbin.org/get?name=musen&age=18"

2、处理方式二:使用params来进行传递
url1 = "http://httpbin.org/get"
data = {
"name":"musen",
"age":18
}
response = requests.get(url=url1,params=data)
print(response.text)

扫描二维码关注公众号,回复: 10060776 查看本文章

猜你喜欢

转载自www.cnblogs.com/erchun/p/12546849.html