cURL与MIME:发布文件

问题描述 投票:0回答:1

问题

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

我一直在使用Mozilla MIME Guide尝试构造HTTP调用,但是不确定在标题和正文中使用什么。

我知道的是:

  • 我希望能够发送任何文件类型,所以我认为我必须使用以下内容:
    • 内容类型:应用程序/八位字节流
    • Content-Disposition:附件; filename =“ filename.xxx”
  • 我的文件使用Base64编码,因此我需要以某种方式编写该文件,然后将其放置在正文中
  • 我想使用分块。这有什么区别吗?

我的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 [email protected]

逻辑应用

enter image description here

api http curl post content-type
1个回答
0
投票

所以我想出了如何将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

逻辑应用程序标题/正文

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.