Fiddler的使用

接口基础知识

根据fiddler捕获接口,分析并编写对应接口测试

测试慕课网账号更换头像的接口

1.先对接口进行抓取

打开fiddler everywhere

image-20241228113033886

来到慕课网个人页面

image-20241228113129673

更换头像

image-20241229160629047

更换头像后

Fiddler抓取到的页面,找到其中的发送请求

image-20241229160712390

根据body值,判断出发送头像的接口

image-20241229160737451

image-20241229160756628

这里乱码,猜测是因为头像为文件,作为值显示时,会乱码

老版的显示为

image-20241229160837229

通过该URL进行编写代码

https://www.imooc.com/user/postpic

根据发送请求的参数对post进行编写

image-20241229161831033

Content-Disposition: form-data; name="fileField"; filename="Snipaste_2024-06-16_12-57-52.jpg"
Content-Type: image/jpeg=����

确定filefield的形式

image-20241229161355377

根据对应的操作,找到对应的、所需要的数据

image-20241229162213063

需要两种数据

  • 第一个是 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,来

image-20241229164241988

修改代码,再次上传

#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复制到地址栏

image-20241229165403607

果然为上传的图片

posted @ 2024-12-30 13:17  清澈的澈  阅读(25)  评论(0)    收藏  举报