如何使用curl上传一个图像文件使用" Content-Type:multipart / related"

时间:2014-09-23 02:58:55

标签: curl

curl --trace trace.txt -X POST -H'Content-Type:multipart / related; boundary = boundary_1234' - data-binary $' - boundary_1234 \ r \ nContent-Type:application / json; charset = UTF-8 \ r \ n \ r \ n {\ r \ n \ t“title”:“TestFile”\ r \ n} \ r \ n \ r \ n - boundary_1234 \ r \ nContent-Type: image / jpeg \ r \ n \ r \ n' - 数据二进制'@ Image0177.jpg' - 数据二进制$'\ r \ n - boundary_1234 - \ r \ n''localhost:3000 / google /上传/驱动/ V2 /文件?uploadType =多

它没有成功,image0177.jpg尺寸17k,但上传谷歌驱动器是6bit,为什么?

2 个答案:

答案 0 :(得分:7)

我认为它是一个相当古老的帖子,但我只是遇到了同样的问题,尝试使用curl的Google Drive API并最终找到解决方案,但它使用起来有点棘手,特别是对内容长度计算,所以我会留下答复,以防它帮助其他人!

根据Google文档,首先,您应该在主要请求中包含Content-Type,Content-Length和Authorization标头。对于Content-Length,您可以让curl自动计算它,所以添加其他标题如下:

-H "Content-Type: multipart/related; boundary=287032381131322"
-H "Authorization: Bearer <Your Oauth2 Access Token>"

其次,您需要包含带有元数据的多部分。此部分需要从边界开始,然后是Content-Type标题:

--287032381131322
Content-Type: application/json; charset=UTF-8

其次是您的元数据内容:

{\"name\":\"Test\"}

下一节是二进制数据的边界开始,它必须再次包含边界定界符和Content-Type标题:

--287032381131322
Content-Type: application/octet-stream

跟随您要上传的二进制数据,简单地传递为:

--data-binary @file.ext

最后,您应该包含一个用于关闭边界定界符的新部分:

--287032381131322--

所以在我的例子中,使用5679字节的文件,这将是完整的命令并从谷歌回复:

curl -v -H "Content-Type: multipart/related; boundary=287032381131322"      \
    -H "Authorization: Bearer <My Oauth2 Access Token>"                   \
    --data-binary $'--287032381131322\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n{\"name\":\"Test\"}\r\n--287032381131322\r\nContent-Type: application/octet-stream\r\n\r\n' \
    --data-binary @b0c11d3b40b71ed08108e5dad7f6ecee-0                       \
    --data-binary $'\r\n--287032381131322--'                                \
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

> POST /upload/drive/v3/files?uploadType=multipart HTTP/1.1
> Host: www.googleapis.com
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type: multipart/related; boundary=287032381131322
> Authorization: Bearer <My Oauth2 Access Token>
> Content-Length: 5848
> Expect: 100-continue
> 
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< X-GUploader-UploadID:  <My Uploader ID>
< Vary: Origin
< Vary: X-Origin
< Content-Type: application/json; charset=UTF-8
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Mon, 01 Jan 1990 00:00:00 GMT
< Date: Mon, 24 Jul 2017 20:56:50 GMT
< Content-Length: 123
< Server: UploadServer
< Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"
< 
{
 "kind": "drive#file",
 "id": "0B-4U01-IEMlATjdfbVNqQURiN0U",
 "name": "Test",
 "mimeType": "application/octet-stream"
}
* Connection #0 to host www.googleapis.com left intact

答案 1 :(得分:0)

更新:有人指出我没有正确地阅读我之前的答案。对于multipart/form-datacurl -F适用于multipart/related 而不是

实际上,curl没有方便的方法来发送我能想到的多部分/相关流。你真的确定这是你想发送的吗?

当您使用命令行时,您已登录整个转移&#39; trace.txt&#39;。没有帮助你找出错误的原因吗?