post man 模拟上传一个文件改成 Python 报错 空文件

2018-08-26 14:20:34 +08:00
 bestehen
import requests

url = "http://yk-project.dev.ethercap.com/report/add"

querystring = {"_":"1111111"}

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"_csrf\"\r\n\r\nBr0949bZlNP4o4iGtNkbE1uE6XU903CqrI9ZnZRQzJF_zEi0n-_g5Zmay_T7qkNYPbbdGVGGRtzA6Qn15Rae6A==\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ReportForm[url]\"; filename=\"齐家网 COO 毛新勇:《未来平台赋能是必然,但要真刀真枪才有意义》-18 页.pdf\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
'Cookie': "u=400; PHPSESSID=dfc4ac95c5c485a49ec167febc0b2da6; _csrf=0226ebc29a0710864db49376b6c400a3895ce0dbfb7161dc3d08e5bc8d148386a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22aoSW7OAr9i2rwVb2H7Khty6qSimxBUMn%22%3B%7D",
'Cache-Control': "no-cache",
'Postman-Token': "64fd1e6e-e442-4161-b32e-110c6627ee23"
}

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

自己复制后改成下列形式 我写绝对路径和相对路径都报错空文件,请指教,这是为啥? 完全复制 postman 的

url = "http://yk-project.dev.ethercap.com/report/add"

querystring = {"_": "1111111"}

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"_csrf\"\r\n\r\n{}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ReportForm[url]\"; filename=\"{}\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--".format(csrf_token,'麦肯锡中国银行业 CEO 季刊-2018 年冬季刊精简版-137 页.pdf')
print(payload.encode('utf-8'))
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
'Cookie': "u=400; PHPSESSID=dfc4ac95c5c485a49ec167febc0b2da6; _csrf=0226ebc29a0710864db49376b6c400a3895ce0dbfb7161dc3d08e5bc8d148386a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22aoSW7OAr9i2rwVb2H7Khty6qSimxBUMn%22%3B%7D",
'Cache-Control': "no-cache",
'Postman-Token': "64fd1e6e-e442-4161-b32e-110c6627ee23"
}

response = requests.request("POST", url, data=payload.encode('utf-8'), headers=headers, params=querystring)
1799 次点击
所在节点    Python
1 条回复
luozic
2018-08-26 17:11:11 +08:00
报错日志在哪? 你这么问谁知道啥问题?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/483336

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX