我找到了这个 API 请求的示例。不幸的是,我没有找到任何其他示例如何将图像上传到 API。 由于我不熟悉 Python,我试图了解如何在 cURL 命令中执行相同的操作。
import requests
auth_headers = {
'app_id': 'your_app_id',
'app_key': 'your_app_key'
}
url = 'https://XXXXXXX'
files = {
'source': open('media/test.jpg')
}
data = {
'timeout': 60
}
response = requests.post(url, files=files, data=data, headers=auth_headers)
我尝试通过尝试 cURL 到 python 转换器来转换它,但我不知道如何使用文件构建它。
最后我想用JAVA来做请求,但我想如果我知道cURL中的请求我就能弄清楚。
希望任何人都可以帮助我。
最佳答案
这样就可以了:
#!/bin/bash
args=(
-H 'app_id: your_app_id'
-H 'app_key: your_app_key'
-F 'source=@/path/to/file'
-F 'timeout=60'
'http://httpbin.org/post'
)
curl "${args[@]}"
或者,作为一句台词:
curl -H 'app_id: your_app_id' -H 'app_key: your_app_key' -F 'source=@/path/to/file' -F 'timeout=60' 'http://httpbin.org/post'
使用-H
指定 header 字段(对每个字段重复)和 -F
指定表单字段 - 作为 key=value
对或 filename=@path
对。使用-F
时,默认为POST
方法,Content-Type
为multipart/form-data
(但这也可以被覆盖)。
关于java - 将带有图像的 Python 请求转换为 cURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47145629/