Upload image from URL through Drive API - http

I'm using Postman to learn how to use the Google Drive API.
I'm trying to upload an API to an image that is in a URL. I'm using the multipart upload described in this documentation link, but the example is not clear to me, I can upload a file with the metadata I'm passing, but the image of the URL I've placed is not being sent.
Please help me understand what I'm doing wrong. Below is the code for my request.
POST /upload/drive/v3/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Content-length: *
Authorization: [myToken]
Content-Type: multipart/related; boundary=test
Cache-Control: no-cache
Postman-Token: [postmanToken]
--test
Content-Type: application/json; charset=UTF-8
{
"name": "imageTest",
"parents": ["1Ij0ZR8yrubkHQaT6tSQNgK6AhW4gbP49"]
}
--test
Content-Type: image/*
https://download.shutterstock.com/gatekeeper/W3siZSI6MTUzNjcwMzQ0NSwiayI6InBob3RvLzEwMDcwNjYwMjMvaHVnZS5qcGciLCJtIjoxLCJkIjoic2h1dHRlcnN0b2NrLW1lZGlhIn0sInR3UkJ0bDZNYUJNUXJ2azZlaE9MbTZlT2VBbyJd/shutterstock_1007066023.jpg
--test--

I doubt that you will be able to use Postman to upload a file. Its going to have to read the file first and then send the file data in chunks.
All of the examples google has for this is using a programming language there is no samples of doing it directly with HTTP calls file upload
If it did work you would be looking at something like this. You are still going to need some language to read the file contents and add it to this.
POST / HTTP/1.1
[[ Less interesting headers ... ]]
Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150
Content-Length: 834
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="text1"
text default
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="text2"
aωb
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain
Content of a.txt.
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html
<!DOCTYPE html><title>Content of a.html.</title>
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file3"; filename="binary"
Content-Type: application/octet-stream
aωb
-----------------------------735323031399963166993862150--

Related

Uploading file gets Bad Request from the server

I am trying to send a file using HTTP from a C++ application (no HTML-boxes). The server keeps answering Code 400/ Bad Request.
To keep it simple, I have changed manually the content of the file to a simple string (later on, I will need to upload real binary files).
the POST request is the following:
POST /post.php HTTP/1.0
Host: posttestserver.com
Accept: */*
Content-Type: multipart/form-data; boundary=BOUNDARY
--BOUNDARY
Content-Disposition: form-data; name="userfile"; filename="example.txt"
Content-Type:text/plain
123ABC
--BOUNDARY--
Connection: close
Any idea what is going on?

Create multipart http post

I have a visual program with which I am trying to create a http multipart post.
I have two context variables that contain, respectively, a string and a file.
The visual program consists of header and body,detached. In the header i insert
Content-Type multipart/form-data,but how can insert the variabile in the body and how can i indicate the different part the body and her name,how can insert in the header?
Your request could be formed as follows:
POST /your_page.html HTTP/1.1
Host: your.host.com
Content-Type: multipart/form-data; boundary=My_Boundary_1234567890
Content-Length: [length of request in bytes]
--My_Boundary_1234567890
Content-Disposition: form-data; name="MyVariableName"
my_valiable_value
--My_Boundary_1234567890
Content-Disposition: form-data; name="MyFile"; filename="picture.jpg"
Content-Type: image/jpeg
binary contents of the file
--My_Boundary_1234567890--
Just do not forget about new line after the last boundary.

Batch request in Google Calendar V3 REST API

How to get/add/update Google Calendar Events using batch request through V3 REST API? I've tried and but not works. According to the docs (https://developers.google.com/google-apps/calendar/batch) it should be possible to send a batch request by posting a multipart/mixed content type message to the API. An example of a working HTTP POST would be great.
Thanks,
Riyaz
I found that the outer URL must be "https://www.googleapis.com/batch/calendar/v3/"
and the boundary url must be "/calendar/v3/calendars/{calendarID}/events"
The full HTTP request looks like:
POST /batch/calendar/v3 HTTP/1.1
Authorization: /*Auth token*/
Host: host
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length
--batch_foobarbaz
Content-ID: 1
GET /calendar/v3/calendars/{calendarID1}/events
--batch_foobarbaz
Content-ID: 2
GET /calendar/v3/calendars/{calendarID2}/events
--batch_foobarbaz--
The following batch request, gets the eventId1, updates the eventId2 and creates a new event under calender that is identified with calendarId.
POST /batch HTTP/1.1
Authorization: /*Auth token*/
Host: host
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:x#barnyard.example.com>
GET /calendar/v3/calendars/calendarId/events/eventId1
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:x#barnyard.example.com>
PUT /calendar/v3/calendars/calendarId/events/eventId2
Content-Type: application/json
Content-Length: part_content_length
{{ body }}
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:x#barnyard.example.com>
POST /calendar/v3/calendars/calendarId/events
Content-Type: application/json
Content-Length: part_content_length
{{ body }}
--batch_foobarbaz--
The endpoint is
https://www.googleapis.com/batch
That works for me when I do Calendar batch requests. One problem I had was with my last boundary token I didn't have the -- after it. So each token starts with -- and the last one has -- at the end. As #Burcu Dogan's example shows.

Issues with valid multipart/mixed and Google Drive

I am attempting to upload a file to Google Drive using the "upload" URL with a type of "multipart". I'm trying to do this without a library and using basic HTTP with a multipart POST. With a body like the following, I am constantly getting the error "Invalid multipart request with 0 mime parts."
The HTTP message looks valid to me. Is there something obvious that I'm missing or doing wrong?
Is there a protocol tester that can verify if my POST body is valid or not?
POST /upload/drive/v2/files?uploadType=multipart HTTP/1.1
Authentiction: Bearer {valid auth_token}
Content-Type: multipart/mixed; boundary="--314159265358979323846"
host: localhost:3004
content-length: 254
Connection: keep-alive
--314159265358979323846
Content-Type: application/json
{"title":"Now","mimeType":"text/plain"}
--314159265358979323846
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Mon Jun 17 2013 20:59:02 GMT-0400 (EDT)
--314159265358979323846--
(The segments look like they have double newlines. I think this is an artifact of the pasting, they are CRLF pairs in the code and appear as a newline when testing, but I guess this could theoretically be the problem, but I'd like proof.)
boundary attribute on the Content-Type header should not include double dashes. Use the following as your Content-Type:
Content-Type: multipart/mixed; boundary="314159265358979323846"

Calculate content length POST

How can I calculate content length for example of:
POST /Upload/ HTTP/1.1
Host: test.lan
User-Agent: Shockwave Flash
Connection: Keep-Alive
Cache-Control: no-cache
Accept: text/*
Content-Length: ?????
Content-Type: multipart/form-data; boundary=----------------------------4d2179e6b3c0
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Filename"
phpinfo.php
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="ASPSESSID"
6e223eb1c7561e9c599f03cc04e9444b
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Filedata"; filename="phpinfo.php"
Content-Type: application/octet-stream
<? phpinfo(); ?>
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Upload"
Submit Query
------------------------------4d2179e6b3c0--
The Content-Length value should be calculated by totaling all data after the termination of the message headers. In the case of your example, this is everything after this point (with CRLF characters included for readability):
...
Content-Length: ?????\r\n
Content-Type: multipart/form-data; boundary=--------------------4d2179e6b3c0\r\n
\r\n
Everything coming after the first empty line (\r\n) -- including your boundary delimiters -- should be counted in the total length. In practice, this usually means that you'll need to tabulate the Content-Length header value after generating the full message entity body. Once you have the full body of the message you can prepend it with your headers to create the full HTTP message.
According to the HTTP spec you aren't technically required to specify the Content-Length header. From RFC 2616 14.13:
Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.
However, this is a pretty standard requirement for most servers which will generally send back an error response if the Content-Length is missing or incorrectly specified.

Resources