发现问题

今天遇到一个问题,文件上传接口,请求头的content-type是这样的:

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryOL6TzQ7JBH992jjw

也就是multipart/form-data格式
数据格式是这样的:

------WebKitFormBoundaryOL6TzQ7JBH992jjw
Content-Disposition: form-data; name="exploit_types"

4
------WebKitFormBoundaryOL6TzQ7JBH992jjw
Content-Disposition: form-data; name="rule_level"

3
------WebKitFormBoundaryOL6TzQ7JBH992jjw
Content-Disposition: form-data; name="file_vid"

41626
------WebKitFormBoundaryOL6TzQ7JBH992jjw
Content-Disposition: form-data; name="pfile"; filename="SunRPC rpc.ypupdated Remote Command Execution (3).pcap"
Content-Type: application/octet-stream

This is file
------WebKitFormBoundaryOL6TzQ7JBH992jjw
Content-Disposition: form-data; name="exploit_value"


------WebKitFormBoundaryOL6TzQ7JBH992jjw
Content-Disposition: form-data; name="valid"

0
------WebKitFormBoundaryOL6TzQ7JBH992jjw
Content-Disposition: form-data; name="create_date"


------WebKitFormBoundaryOL6TzQ7JBH992jjw
Content-Disposition: form-data; name="modify_date"

2020-06-23
------WebKitFormBoundaryOL6TzQ7JBH992jjw--

解决问题

emmm,遇事不决,量子力学,暴力美学
直接上 request试试呗

import requests
 
url = 'https://www.iflyrec.com/XFTJService/web/audio/upload?folder=1416861992900653'
files = {'file': open('video.wav', 'rb')}
 
r = requests.post(url, files=files)
print(r.text)

很好,报错了,于是查了一下资料,发现 multipart/form-data 请求在 requests里面其实是有实例的,使用元组形式上传files,于是完成了以下代码:

import requests

headers = {
    "Host": "www.iflyrec.com",
    "Connection": "keep-alive",
    "Content-Length": "137110",
    "Origin": "https://www.iflyrec.com",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6776.400 QQBrowser/10.3.2601.400",
    "Accept": "*/*",
    "Referer": "https://www.iflyrec.com/html/addArtificialOrder.html",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    # "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryZjPRjN0wAzGytw34",
    "Cookie": "login_err=0; from=baidu; keywords=%25e8%25ae%25af%25e9%25a3%259e+%25e8%25af%25ad%25e9%259f%25b3%25e8%25af%2586%25e5%2588%25ab; Hm_lvt_aee4c3bed3d9924f2f184904ecb37065=1542615755; Hm_lvt_c711619f2461980a3dbceed5874b0c6a=1542615755; storage=QsIwv8WBIrqRiBa62wvKbo8oswgQSz4Wr4bjQk8jm+scRW7Z6rVZZv1WwElHU0BN+ZAmvzlXvxc2gaeHLi2yHQ==; ec_im_local_status=0; CUSTOM_INVITE_CONTENT=; ec_invite_state=0; Hm_lpvt_aee4c3bed3d9924f2f184904ecb37065=1542615766; ec_im_tab_num=1; ec_invite_state_time=1542615765582; Hm_lpvt_c711619f2461980a3dbceed5874b0c6a=1542615766"
}

# files = {'file': open('333.wav', 'rb')}

files = {'id': (None, 'WU_FILE_0'), 
         'name':  (None, '333.wav'), 
         'type': (None, 'audio/wav'),
         'lastModifiedDate': (None, 'Tue Aug 21 2018 17:22:12 GMT+0800 (中国标准时间)'),
         'size': (None,'136380'), 
         'file': ('333.wav', open('333.wav', 'rb'), 'audio/wav')} # 

r = requests.post('https://www.iflyrec.com/XFTJService/web/audio/upload?folder=1416861992900653', headers=headers, files=files)

print(r.text)
print(r.headers)
print(r.status_code)

接着试了一下

好了,成功

注意

需要注意的是,我将headers请求头里面的content-type属性注释了,如果加上了,则会报错,然后我把这个模拟请求抓包下来看了看,他自动加上了Content-Type: multipart/form-data; boundary=${bound},所以这个boundary应该是上传文件的标识,上传文件的时候content-type会有一个默认值,我们不去指定,也就没问题了。

最后修改:2020 年 08 月 05 日
如果觉得我的文章对你有用,请随意赞赏