如何在没有任何 url 的情况下检索通过 Postman 发送的 python 中的文件对象
How to retrieve file object in python sent via Postman without any url
我正在通过邮递员 POST 或 PUT API 将文件作为对象发送,如下所示:
我怎样才能在Python-
- 获取此文件对象
- 读取并保存
如果您在 Postman 中有工作请求,您可以以 Python - Requests
格式复制自动生成的 Code Snippet
:
它可能看起来像这样:
import requests
url = "localhost:8080"
payload="<file contents here>"
headers = {
'Content-Type': 'application/octet-stream'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
我最终在没有 url 的情况下实现了这个问题的答案 -
@app.route('/uploadFIle', methods=['PUT'])
def uploadFile():
chunk_size = 4096
with open("/Users/xyz/Documents/filename", 'wb') as f:
while True:
chunk = request.stream.read(chunk_size)
if len(chunk) == 0:
break
f.write(chunk)
return jsonify({"success":"File transfer initiated"})
我正在通过邮递员 POST 或 PUT API 将文件作为对象发送,如下所示:
我怎样才能在Python-
- 获取此文件对象
- 读取并保存
如果您在 Postman 中有工作请求,您可以以 Python - Requests
格式复制自动生成的 Code Snippet
:
它可能看起来像这样:
import requests
url = "localhost:8080"
payload="<file contents here>"
headers = {
'Content-Type': 'application/octet-stream'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
我最终在没有 url 的情况下实现了这个问题的答案 -
@app.route('/uploadFIle', methods=['PUT'])
def uploadFile():
chunk_size = 4096
with open("/Users/xyz/Documents/filename", 'wb') as f:
while True:
chunk = request.stream.read(chunk_size)
if len(chunk) == 0:
break
f.write(chunk)
return jsonify({"success":"File transfer initiated"})