Created
August 15, 2014 13:12
-
-
Save sergiusens/7f75c3593710e1f40abc to your computer and use it in GitHub Desktop.
gmail batch request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: Sergio Schvezov | |
Calling this will dump a response similar to: | |
HTTP/1.1 200 OK | |
Transfer-Encoding: chunked | |
Alternate-Protocol: 443:quic | |
Cache-Control: no-cache, no-store, max-age=0, must-revalidate | |
Content-Type: multipart/mixed; boundary=batch_mtD6I8ONpCM=_AAPHSI2gDBg= | |
Date: Fri, 15 Aug 2014 13:06:49 GMT | |
Expires: Fri, 01 Jan 1990 00:00:00 GMT | |
Pragma: no-cache | |
Server: GSE | |
X-Content-Type-Options: nosniff | |
X-Frame-Options: SAMEORIGIN | |
X-Xss-Protection: 1; mode=block | |
1df0 | |
--batch_mtD6I8ONpCM=_AAPHSI2gDBg= | |
Content-Type: application/http | |
HTTP/1.1 200 OK | |
ETag: "QLs4pSnnOYcKsc2Nteng63h1s-w/R0OLs2HgEc9fXAJB-Z33SOeIrJk" | |
Content-Type: application/json; charset=UTF-8 | |
Date: Fri, 15 Aug 2014 13:06:49 GMT | |
Expires: Fri, 15 Aug 2014 13:06:49 GMT | |
Cache-Control: private, max-age=0 | |
Content-Length: 7291 | |
{ | |
"messages": [ | |
{ | |
"id": "XXXXXX", | |
"threadId": "XXXXXXXX" | |
}, | |
... | |
... | |
... | |
], | |
"nextPageToken": "12798809368304798032", | |
"resultSizeEstimate": 624 | |
} | |
--batch_mtD6I8ONpCM=_AAPHSI2gDBg=-- | |
0 | |
*/ | |
package main | |
import ( | |
"bytes" | |
"fmt" | |
"mime/multipart" | |
"net/http" | |
"net/http/httputil" | |
"net/textproto" | |
"os" | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("usage:", os.Args[0], "[access_token]") | |
return | |
} | |
accessToken := os.Args[1] | |
buffer := new(bytes.Buffer) | |
multipartW := multipart.NewWriter(buffer) | |
mh := make(textproto.MIMEHeader) | |
mh.Set("Content-Type", "application/http") | |
// mh.Set("Content-Id", "list") | |
partW, err := multipartW.CreatePart(mh) | |
if err != nil { | |
fmt.Println("cannot create part:", err) | |
return | |
} | |
content := []byte("GET https://www.googleapis.com/gmail/v1/users/me/messages HTTP/1.1\r\n") | |
partW.Write(content) | |
multipartW.Close() | |
req, err := http.NewRequest("POST", "https://www.googleapis.com/batch", buffer) | |
if err != nil { | |
fmt.Println("cannot create batch request:", err) | |
return | |
} | |
req.Header.Set("Authorization", "Bearer "+accessToken) | |
req.Header.Set("Content-Type", "multipart/mixed; boundary="+multipartW.Boundary()) | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
fmt.Println("error while requesting batch:", err) | |
return | |
} | |
respDump, err := httputil.DumpResponse(resp, true) | |
if err != nil { | |
fmt.Println("failed to dump response:", err) | |
return | |
} | |
fmt.Println(string(respDump)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment