How to use curl to upload a single image file using "Content-Type: multipart / related"

curl --trace trace.txt -X POST -H 'Content-Type: multipart / related; border = border_1234 '--data-binary $' - border_1234 \ r \ nContent-Type: application / json; charset = UTF-8 \ r \ n \ r \ n {\ r \ n \ t "title": "TestFile" \ r \ n} \ r \ n \ r \ n - border_1234 \ r \ nContent-Type: image / jpeg \ r \ n \ r \ n '--data-binary' @ Image0177.jpg '--data-binary $' \ r \ n - border_1234 - \ r \ n '' localhost: 3000 / google / downloads / drive / v2 / files? uploadType = multi-part

It fails, image0177.jpg size is 17k , but download Google Drive 6bit , why?

+3
source share
2 answers

I see this pretty old post, but I just ran into the same problem trying to use the Google Drive API with curl and finally found a solution, but it is a little difficult to use, especially when calculating the length of the content, so I will leave an answer if it helps someone else!

According to Google documentation, first of all, you should include the Content-Type, Content-Length and Authorization headers in the main request. For Content-Length, you can let curl automatically calculate it, so add other headers like this:

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

Secondly, you need to include a multi-page section with metadata. This section should begin with a border followed by a Content-Type header:

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

According to your metadata content:

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

The next section is the boundary start for binary data, which should again contain the border delimiter and the Content-Type header:

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

The following are the binary data you want to download, which is simply passed as:

 --data-binary @file.ext 

Finally, you must include a new section to close the border delimiter:

 --287032381131322-- 

So, in my example, using a file of 5679 bytes, this will be the complete command and response 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 
+3
source

Update: It was pointed out that I did not read the question correctly for my previous answer. curl -F for multipart/form-data not for multipart/related .

In fact, curl does not have a convenient way to send a multi-part / linked stream that I can think of. Are you sure you want to send?

When you used your command line, you got all the transfer registered in 'trace.txt'. Didn't that help you figure out what's wrong?

0
source

Source: https://habr.com/ru/post/1012950/


All Articles