回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我在使用python请求上传一些图像时遇到问题。我试图上传照片的网站是<a href="https://www.myauto.ge/en/add" rel="nofollow noreferrer">myauto</a>。<a href="https://i.stack.imgur.com/rmgDv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rmgDv.png" alt="enter image description here"/></a></p>
<p>当我单击它并选择“图像”时,我可以在浏览器的“网络”选项卡中看到此端点上发送的请求:</p>
<pre><code>https://static.my.ge/
</code></pre>
<p>这是从浏览器发送的表单数据<br/>
<a href="https://i.stack.imgur.com/0Ty6r.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0Ty6r.jpg" alt="enter image description here"/></a></p>
<p>要查看完整的来源:
<a href="https://i.stack.imgur.com/4i1Dv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4i1Dv.jpg" alt="enter image description here"/></a></p>
<p>首先,我尝试使用以下脚本发布数据:</p>
<pre><code>image_data = (
# 'Files[]: (binary)
('do', 'Files'),
('Func', 'UploadPhotos'),
('SiteID', 1),
('UserID', 4134977),
('IP', 'XX.XX.XX.XX'),
('UploadedFiles', 0)
)
# NOTE - we can have multiple `Files[]`
image_urls = tuple()
image_urls += (('Files[]', 'some_image.jpg'),)
response = requests.request(
method='POST',
url='https://static.my.ge/',
data=self.image_data + image_urls,
headers=headers
)
</code></pre>
<p>我正在使用的标题:</p>
<pre><code>headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Accept-Encoding': "gzip, deflate, br",
'Accept': "*/*",
'Connection': "keep-alive",
'Host': "static.my.ge",
'Origin': "https://www.myauto.ge",
'Referer': "https://www.myauto.ge/ka/add",
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "GoogleChrome";v="90"',
'sec-ch-ua-mobile': "?0",
'Sec-Fetch-Dest': "empty",
'Sec-Fetch-Mode': "cors",
'Sec-Fetch-Site': "cross-site",
'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
'cache-control': "no-cache"
}
</code></pre>
<p>但我一直得到以下回应:</p>
<pre><code>{'StatusID': 0, 'StatusCode': 0, 'Message': 'Incorrect Func'}
</code></pre>
<p>这是期望的输出:</p>
<pre><code>"StatusID": 0,
"StatusCode": 1,
"Message": "Error occurred during the operation",
"Data": {
"FilesList": [
"https://static.my.ge/tmp/6db262b3d7a2f9bfd56618640b6deed8_thumbs.jpg"
],
"imgKey": [
"6db262b3d7a2f9bfd56618640b6deed8"
]
}
</code></pre>
<p>然后我尝试生成相同的Webkit表单Boundry并将原始字符串传递给数据,但始终得到相同的结果。我还尝试发送图像blob或base64格式,但仍然收到相同的错误</p>
<p>然后我尝试使用postman在这个端点上发送请求,它成功地返回了我所期望的数据。我在正文部分选择了表单数据格式,并通过邮递员上传了文件。然后我尝试查看请求的日志,并尝试从postman复制它并使用python脚本运行,但没有成功,仍然得到相同的错误。最后,我尝试从postman代码生成器复制代码片段,但仍然没有成功。我不明白发生了什么事,也没有办法了</p>
<p><strong>编辑:</strong></p>
<p>以下是邮递员脚本:</p>
<pre><code>url = "https://static.my.ge"
payload = (
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"Files[]\"; filename=\"some_image.jpg\"\r\n"
"Content-Type: image/jpeg\r\n\r\n\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"do\"\r\n\r\nFiles\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"SiteID\"\r\n\r\n1\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"UserID\"\r\n\r\n1902119\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"IP\"\r\n\r\nXX.XX.XX.XX\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"UploadedFiles\"\r\n\r\n0\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"Func\"\r\n\r\nUploadPhotos\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW--"
)
response = requests.request("POST", url, data=payload, headers=headers)
</code></pre>
<p>此外,我们还发现,这就是图像在请求中的表示方式。不确定如何在python代码中执行相同操作:
<a href="https://i.stack.imgur.com/kxeeb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kxeeb.png" alt="enter image description here"/></a></p>
<p><strong>更新:</strong>
试图修改邮递员生成的脚本:</p>
<pre><code> payload = (
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"Files[]\"; filename=\"blob\"\r\n"
"Content-Type: image/jpg\r\n\r\n"
f"{image}\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"do\"\r\n\r\nFiles\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"SiteID\"\r\n\r\n1\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"UserID\"\r\n\r\n1902119\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"IP\"\r\n\r\nXX.XX.XX.XX\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"UploadedFiles\"\r\n\r\n0\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: "
"form-data; name=\"Func\"\r\n\r\nUploadPhotos\r\n"
"------WebKitFormBoundary7MA4YWxkTrZu0gW--"
)
</code></pre>
<p>尝试将base64和二进制图像替换为<code>f{"image"}</code>。但从服务器收到新的错误消息,不确定是否有用</p>
<pre><code>object(Exception)#4 (7) {
["message":protected]=>
string(34) "Invalid image file: /tmp/phpFMI89k"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(7)
["file":protected]=>
string(58) "/datastore/web/static.my.ge/htdocs/libs/SimpleImageNew.php"
["line":protected]=>
int(130)
["trace":"Exception":private]=>
array(3) {
[0]=>
array(6) {
["file"]=>
string(44) "/datastore/web/static.my.ge/htdocs/index.php"
["line"]=>
int(1300)
["function"]=>
string(8) "fromFile"
["class"]=>
string(14) "SimpleImageNew"
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
string(14) "/tmp/phpFMI89k"
}
}
[1]=>
array(6) {
["file"]=>
string(44) "/datastore/web/static.my.ge/htdocs/index.php"
["line"]=>
int(494)
["function"]=>
string(12) "UploadPhotos"
["class"]=>
string(7) "_Static"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[2]=>
array(6) {
["file"]=>
string(44) "/datastore/web/static.my.ge/htdocs/index.php"
["line"]=>
int(3066)
["function"]=>
string(7) "SetFunc"
["class"]=>
string(7) "_Static"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
}
["previous":"Exception":private]=>
NULL
}
</code></pre>