使用 HTTPie 的 multipart/form-data 中一个文件的内容类型

Flo*_*ges 6 httpie

我使用HTTPie来 POST 多部分/表单数据请求(传递选项-f)。它包括一个文件字段(使用选项@)。多部分请求的相应部分有一个pseudo-header Content-Disposition,但没有pseudo-header Content-Type

如何Content-Type为特定文件添加这样的?

为了完整起见,以下是我现在发送的内容(如图所示-p B):

--xyz
Content-Disposition: form-data; name="file"; filename="file.txt"

Hello, world!
This is the content of the file.

--xyz
Run Code Online (Sandbox Code Playgroud)

但这是我需要发送的内容:

--xyz
Content-Disposition: form-data; name="file"; filename="file.txt"
Content-Type: text/plain

Hello, world!
This is the content of the file.

--xyz
Run Code Online (Sandbox Code Playgroud)

或者换句话说,就像 CURL 等效选项:-F "file=file.txt;type=text/plain"

Ond*_*ert 6

这对我有用:

http -f POST :8080/submit-file metadata=@metadata.json files@file1.jpeg files@file1.jpeg
Run Code Online (Sandbox Code Playgroud)

该文档帮助了我: https: //httpie.org/doc#file-upload-forms

=@使得 httpie 以 content type 发送它text/plain


Flo*_*ges 2

我刚刚注意到现在的文档multipart/form-data如下所示(在最后):

上传文件时,会根据文件名推断其内容类型。您可以手动覆盖推断的内容类型:

$ http -f POST httpbin.org/post name='John Smith' cv@'~/files/data.bin;type=application/pdf'

我不知道那是什么时候发生的,但现在看来:

  1. Content-Type自动推断“部分”
  2. 可以从命令行选项覆盖它

这就是我一直在寻找的一切!:-)

参考: https: //httpie.org/docs#file-upload-forms