接口基础知识
根据fiddler捕获接口,分析并编写对应接口测试
测试慕课网账号更换头像的接口
1.先对接口进行抓取
打开fiddler everywhere
来到慕课网个人页面
更换头像
更换头像后
Fiddler抓取到的页面,找到其中的发送请求
根据body值,判断出发送头像的接口
这里乱码,猜测是因为头像为文件,作为值显示时,会乱码
老版的显示为
通过该URL进行编写代码
https://www.imooc.com/user/postpic
根据发送请求的参数对post进行编写
Content-Disposition: form-data; name="fileField"; filename="Snipaste_2024-06-16_12-57-52.jpg"
Content-Type: image/jpeg=����
确定filefield的形式
根据对应的操作,找到对应的、所需要的数据
需要两种数据
- 第一个是 name=“fileField”, type = “file”, 接受的类型也给出
- 第二个是 name=“type”, value=”1”
进行代码编写
#coding=utf-8
import requests
import json
#上传文件
url = 'https://www.imooc.com/user/postpic'
# file = {
# "fileField":("文件名称",open("路径","rb"),"image/jpg"),
# "type":"1"
# }
file = {
"fileField":("Snipaste_2024-06-16_12-57-52.jpg",open("D:/Document/Snipaste_2024-06-16_12-57-52.jpg","rb"),"image/jpg"),
"type":"1"
}
res = requests.post(url, files=file, verify=False).text
print(res)
res = requests.post(url, files=file, verify=False).json()
print(res)
结果
{"result":0,"data":"","msg":"\u8bf7\u767b\u5f55"}
{'result': 0, 'data': '', 'msg': '请登录'}
说明没有登录,无法上传图片
使用cookie,来
修改代码,再次上传
#coding=utf-8
import requests
import json
#上传文件
url = 'https://www.imooc.com/user/postpic'
# file = {
# "fileField":("文件名称",open("路径","rb"),"image/jpg"),
# "type":"1"
# }
file = {
"fileField":("Snipaste_2024-06-19_19-47-41.jpg",open("D:/Document/Snipaste_2024-06-19_19-47-41.jpg","rb"),"image/jpg"),
"type":"3"
}
cookie = {
"apsid":"llMjQ5ZDg3OTNkODExNjJmNjBkNWE4Mzk4Mzg1MGQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANzI4MjExNgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyMzExMTI5MDQ5M0BxcS5jb20AAAAAAAAAAAAAAAAAADQ4Y2VjYzZkMzAzOTM3MDhiMDVjNmU0MmQ1MzljZGM4qwFxZ%2BqWHGM%3DZD"
}
res = requests.post(url, files=file, cookies=cookie, verify=False).text
print(res)
# with open("mukewang.apk","wb") as f:
# f.write(res.content)
#res = requests.post(url,files=file,cookies=cookie,verify=False).json()
# print(res)
# #res = requests.post(url,files=file,cookies=cookie,verify=False).json()
# print(res)
结果
{"result":1,"data":{"key":"67710ded0001738b03050458","imgpath":"\/\/img1.sycdn.imooc.com\/67710ded0001738b03050458.jpg"},"msg":""}
将url复制到地址栏
果然为上传的图片
标签:Fiddler,url,res,jpg,file,使用,requests,post From: https://www.cnblogs.com/lmc7/p/18640812