我使用Flask编写了一个非常简单的API,我想使用POST命令将文件上传到其中。我可以使用cURL轻松地使它工作,但使用逻辑应用程序则不能那么多。
我一直在使用Mozilla MIME Guide尝试构造HTTP调用,但是不确定在标题和正文中使用什么。
我知道的是:
from flask import Flask, request, redirect
app = Flask(__name__)
@app.route('/', methods=['POST'])
def print_hello():
if request.files:
request.files['file'].save("/home/ebbemonster/cool_file.txt")
return "Hello World"
return "Goodbye World"
if __name__=="__main__":
app.run(host='0.0.0.0')
curl -X POST 13.81.62.87:5000 -F [email protected]
所以我想出了如何将cURL POST转换为HTTP标头/正文。
# Logging post call
curl -X POST XX.XX.XX.XX:5000 -F [email protected] --trace-ascii post.log
# Fetching header
head -n 30 post.log
=> Send header, 216 bytes (0xd8)
0000: POST / HTTP/1.1
0011: Host: XX.XX.XX.XX:5000
0029: User-Agent: curl/7.58.0
0042: Accept: */*
004f: Content-Length: 300745456
006a: Content-Type: multipart/form-data; boundary=--------------------
00aa: ----ec1aab65fb2d68fd
00c0: Expect: 100-continue
# Fetching body
sed -n '18,25p' post.log
0000: --------------------------ec1aab65fb2d68fd
002c: Content-Disposition: form-data; name="file"; filename="GH019654.
006c: MP4"
0072: Content-Type: application/octet-stream
009a:
009c: ....ftypmp41 [email protected]
00dc: 517...................................1...US.8'.f..C328132710684
011c: 1.HERO7 Black........................E.....1...US.8'.f..........
# Fetching end of body
tail -n 30 google.log
02c2: --------------------------ec1aab65fb2d68fd--
== Info: HTTP 1.0, assume close after body