Python 请求将 GIF 发布为八进制流而不是正确的 MIME 类型



我正在尝试在python3请求中复制以下curl调用。 但是,当它发送文件时,由于某种原因,它的 mimeType 作为大小为 0 的应用程序/八位字节流发送。 我已经尝试了我能想到的请求调用的所有变体,我什至尝试了对 mimeType 和大小进行硬编码,但它不会改变我的 Web 服务器看到的请求。

这是我想要复制的卷曲调用:

curl -X POST 
http://myurl/api/v1/content/createFile 
-H 'cache-control: no-cache' 
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' 
-H 'postman-token: c35cc623-2cd8-1143-ba64-5870156f7498' 
-F apiKey=ABCD 
-F file=@bear.gif

这是我正在尝试的python代码:

import requests
files = {
'apiKey': (None, 'ABCD'),
'file': ('bear.gif', open('bear.gif', 'rb')),
}
url = 'http://myurl/api/v1/content/createFile'
response = requests.post(url, files=files)

这是我的 Web 服务器从 curl 调用中看到的内容:

"apiKey" => "ABCD"
"file" => UploadedFile {#323 ▼
-test: false
-originalName: "bear.gif"
-mimeType: "image/gif"
-size: 1283057
-error: 0
#hashName: null
path: "/tmp"
filename: "phpizaC5J"
basename: "phpizaC5J"
pathname: "/tmp/phpizaC5J"
extension: ""
realPath: "/tmp/phpizaC5J"
aTime: 2018-06-01 16:20:30
mTime: 2018-06-01 16:20:32
cTime: 2018-06-01 16:20:32
inode: 1441804
size: 1283057
perms: 0100600
owner: 48
group: 48
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}

这是我的网络服务器在我从python请求中发布时看到的内容:

"apiKey" => "ABCD"
"file" => UploadedFile {#323 ▼
-test: false
-originalName: "bear.gif"
-mimeType: "application/octet-stream"
-size: 0
-error: 1
#hashName: null
path: ""
filename: ""
basename: ""
pathname: ""
extension: ""
realPath: "/var/www/html/myurl"
aTime: 1970-01-01 00:00:00
mTime: 1970-01-01 00:00:00
cTime: 1970-01-01 00:00:00
inode: false
size: false
perms: 00
owner: false
group: false
type: false
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}

我做错了什么?

经过多次试验和错误,我发现这实际上是由于 PHP Web 服务器的upload_max_filesize设置得太低。

我的假设是该文件被截断,因此它不再是有效的 GIF。 但是,我不打算研究真正的原因,因为这不再是问题。

最新更新