如何使用curl使用“内容类型:多部分/相关”上传一个图像文件。 [英] How to use curl upload one image file using "Content-Type: multipart/related"
问题描述
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\n内容类型:image / jpeg\r\n\r\n'--data-binary'@ Image0177.jpg'--data-binary $'\ \r\n--boundary_1234--\r\n''localhost:3000 / google / upload / drive / v2 / files?uploadType = multipart
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' --data-binary '@Image0177.jpg' --data-binary $'\r\n--boundary_1234--\r\n' 'localhost:3000/google/upload/drive/v2/files?uploadType=multipart
它并没有成功, image0177.jpg
大小 17k
,但上传的Google驱动器是 6bit
,为什么?
It dit not succeed, image0177.jpg
size 17k
, but upload google drive is 6bit
, why?
推荐答案
我看到这是一个很老的帖子,但是我在尝试时遇到了同样的问题使用带有curl的Google Drive API并最终找到了解决方案,但是使用起来有点棘手,特别是在内容长度计算上,因此我会在回复中说明是否有帮助!
I see it's a quite old post, but I just faced the same issue trying to use Google Drive API with curl and finally found the solution, but it's a bit tricky to use, specially on the content length calculation, so I will leave a reply in case it helps someone else!
根据google文档,首先,您应在主请求中包含Content-Type,Content-Length和Authorization标头。对于Content-Length,您可以让curl自动计算它,因此添加其他标头如下:
According to google documentation, first of all, you should include the Content-Type, Content-Length and Authorization headers in the main request. For the Content-Length, you can let curl automatically calculate it, so add the other headers like this:
-H "Content-Type: multipart/related; boundary=287032381131322"
-H "Authorization: Bearer <Your Oauth2 Access Token>"
第二,您需要在元数据中包含一个多部分。此部分需要从边界开始,然后是Content-Type标头:
Second, you need to include a multipart section with the metadata. This section need to start with the boundary, followed by the Content-Type header:
--287032381131322
Content-Type: application/json; charset=UTF-8
后跟您的元数据内容:
{\"name\":\"Test\"}
下一部分是二进制数据的边界起点,它必须再次包含边界定界符和Content-Type标头:
Next section, is the boundary start for the binary data, which must contain again the boundary delimiter and the Content-Type header:
--287032381131322
Content-Type: application/octet-stream
后跟要上传的二进制数据,只需将其传递为:
Followed by the binary data you want to upload, which is simply passed as:
--data-binary @file.ext
最后,您应该包括一个用于关闭边界定界符的新部分:
And lastly, you should include a new section for closing the boundary delimiter:
--287032381131322--
因此在我的示例中,使用5679字节的文件,这将是完整的命令并由Google答复:
So in my example, using a file of 5679 bytes, this will be the full command and reply from google:
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
这篇关于如何使用curl使用“内容类型:多部分/相关”上传一个图像文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!