0

我无法使用文件 API 上传文件

我在 POSTMAN 和 Curl 中尝试了这个 HTTP 请求,但没有成功,并且两者的结果相同: 将文件(图片)附加到对话中

您能否分享一个来自 Postman 的真实工作示例,或者从 Postman 转换为我可以导入的 Curl 代码片段?

以下返回“设置了错误的 Content-Disposition 标头”

POST /rest/v2/fileapi HTTP/1.1
Host: circuitsandbox.net
Authorization:  Bearer MyTokenCodeGoesHere
Content-Length:  100
Content-Disposition:  attachment; filename="test.txt"
Cache-Control:  no-cache

MyBinaryCodeGoesHere

上面在 curl 中看起来像这样:

curl --location --request POST "https://circuitsandbox.net/rest/v2/fileapi" \
    --header "Authorization:  Bearer MyTokenCodeGoesHere" \
    --header "Content-Length:  100" \
    --header "Content-Disposition:  attachment; filename=\"test.txt\"" \
    --header "Cache-Control:  no-cache" \
    --header "MyBinaryCodeGoesHere: "

Host: local.circuit.com代替 进行测试Host: circuitsandbox.net,没有连接,我认为这只是一个例子,但为了以防万一。

预期的:

{"fileId":"fb211fd6-df53-4b82-824d-986dac47b3e7","attachmentId":"ZmIyMT..."}

实际结果:

“设置了错误的 Content-Disposition 标头”

4

2 回答 2

1

这是一个发布 json 文档的 curl 示例:

curl -X POST https://circuitsandbox.net/rest/v2/fileapi \
 -H 'authorization: Bearer <token>' \
 -H 'cache-control: no-cache' \
 -H 'content-disposition: attachment; filename="test.json"' \
 -H "Content-Type: application/json" \
 -d '{"key":"val"}'

使用邮递员,您可以轻松地在正文的二进制选项卡中设置要上传的文件。您需要的唯一标题是“授权”和“内容处置”。“Content-Disposition”标头的格式为:附件;文件名="test.log"

在您的示例中,数据看起来不正确。它不应该在标题中传递。

于 2019-08-28T03:41:21.733 回答
0

只是为任何可能觉得这很有用的人加起来。

这是我的 HTTP 代码:

POST /rest/v2/fileapi HTTP/1.1
Host: circuitsandbox.net
Authorization:  Bearer <token>
Cache-Control:  no-cache
Content-Disposition:  attachment; filename="test.txt"
Content-Type: text/plain

{"src":"/C:/Temp/test.txt"}

这是我的卷曲代码:

curl --location --request POST "https://circuitsandbox.net/rest/v2/fileapi" \
    --header "Authorization:  Bearer <token>" \
    --header "Cache-Control:  no-cache" \
    --header "Content-Disposition:  attachment; filename=\"test.txt\"" \
    --header "Content-Type: text/plain" \
    --data-binary "@C:\Temp\test.txt"

对于阅读本文的任何人,这里是 MIME 类型的列表,您的内容类型会根据您要上传的文档类型而变化:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types

更多关于 -d (--data-binary): https://bagder.gitbooks.io/everything-curl/http-post.html

于 2019-08-28T16:31:45.950 回答