Content-Type 对于 multipart/form-data 中的一个文件,使用 HTTPie

Content-Type for one file in multipart/form-data with HTTPie

我用HTTPie来POST一个 multipart/form-data 请求(传递 -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

但这是我需要发送的:

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

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

--xyz

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

httpie 做不到这一点。有一个 issue for it,有一些修复它的拉取请求,但是 none 已经被合并了。

这对我有用:

http -f POST :8080/submit-file metadata=@metadata.json files@file1.jpeg files@file1.jpeg

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

=@ 使 httpie 发送内容类型为 text/plain

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

When uploading files, their content type is inferred from the file name. You can manually override the inferred content type:

$ 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.