cURL 与 MIME:发布文件

cURL vs MIME: POSTing a file

问题

我使用 Flask 编写了一个非常简单的 API,我想使用 POST 命令将文件上传到它。我可以使用 cURL 轻松地使其工作,但使用逻辑应用程序就没那么多了。

我一直在使用 Mozilla MIME Guide 尝试构造 HTTP 调用,但我不确定在 header 和 body 中使用什么。

我知道的是:

我的API

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

curl -X POST 13.81.62.87:5000 -F file=@GH019654.MP4

逻辑应用程序

所以我想出了如何将 cURL POST 转换为 HTTP header/body。

正在获取 header/body 详细信息

# Logging post call
curl -X POST XX.XX.XX.XX:5000 -F file=@GH019654.MP4 --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 ...mp41....mdatGPRO@...HD7.01.01.70.00LAJ9022436601
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

逻辑应用程序 Header/Body