2018/3/26 Discontinuing support for JSON-RPC and Global HTTP Batch Endpoints
According to the information above, the endpoint of the Batch Request will change. Those who are using, please note the content below (until 2019/3/25).
1 2 3 4 | www.googleapis.com/batch ↓↓↓ www.googleapis.com/batch/<api>/<version>. |
In Google API, there is a structure that allows implementation of many APIs called Batch Request .
Using Batch Request will improve API provider performance . When the number of requests decreases, the load on Google will certainly decrease, so basically this structure has no minus points.
When you have to implement many APIs, take full advantage of Batch Request!
In addition, the structure of BatchRequest is similar to firing mail or sending attach files of Web.
@ This article is to help you understand the structure of Batch Request. When used in practice, it is also available in libraries of the types Google provides, so I would recommend it. https://developers.google.com/discovery/libraries
Predestined
Regarding getting access token, please refer to this article. http://qiita.com/shin1ogawa/items/49a076f62e5f17f18fe5
In this article, ACCESS_TOKEN will be added to the environment variable as follows.
1 2 | export ACCESS_TOKEN=ya29.fAIcwrExRp.... |
Release the API as usual
Before BatchRequest will try to execute the API as usual.
How to submit the API
<dl> <dt> URL </dt> <dd> relies on API types </dd> <dt> Method </dt> <dd> relies on API types, mostly 主 に GET 、 POST 、 PATCH 、 DELETE </dd> <dt> Authorization Header </dt> <dd> Authorization: Bearer {ACCESS_TOKEN} </dd> <dt> Body </dt> <dd> Used in POST and PATCH, mostly JSON </dd> </dl>
For example, use Drive API to get list
Request
1 2 | curl https://www.googleapis.com/drive/v3/files -H "Authorization: Bearer $ACCESS_TOKEN" |
Response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <span class="token punctuation">{</span> <span class="token property">"kind"</span> <span class="token operator">:</span> <span class="token string">"drive#fileList"</span> <span class="token punctuation">,</span> <span class="token property">"files"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token property">"kind"</span> <span class="token operator">:</span> <span class="token string">"drive#file"</span> <span class="token punctuation">,</span> <span class="token property">"id"</span> <span class="token operator">:</span> <span class="token string">"1q5eHxwMySsmnAGV4x831txwWVZ6DDD1CmwCP4QcRBAs"</span> <span class="token punctuation">,</span> <span class="token property">"name"</span> <span class="token operator">:</span> <span class="token string">"Document 001"</span> <span class="token punctuation">,</span> <span class="token property">"mimeType"</span> <span class="token operator">:</span> <span class="token string">"application/vnd.google-apps.document"</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token property">"kind"</span> <span class="token operator">:</span> <span class="token string">"drive#file"</span> <span class="token punctuation">,</span> <span class="token property">"id"</span> <span class="token operator">:</span> <span class="token string">"1xXnBefD_jDcjZ-vEb50LO7xS01-AsHgdpKioQLegHrQ"</span> <span class="token punctuation">,</span> <span class="token property">"name"</span> <span class="token operator">:</span> <span class="token string">"Spreadsheet 001"</span> <span class="token punctuation">,</span> <span class="token property">"mimeType"</span> <span class="token operator">:</span> <span class="token string">"application/vnd.google-apps.spreadsheet"</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token property">"kind"</span> <span class="token operator">:</span> <span class="token string">"drive#file"</span> <span class="token punctuation">,</span> <span class="token property">"id"</span> <span class="token operator">:</span> <span class="token string">"1NT8jIxP_NKbzgdJyQ4FPJTFaiAROmKZmIPq_TBS8wJY"</span> <span class="token punctuation">,</span> <span class="token property">"name"</span> <span class="token operator">:</span> <span class="token string">"Presentation 001"</span> <span class="token punctuation">,</span> <span class="token property">"mimeType"</span> <span class="token operator">:</span> <span class="token string">"application/vnd.google-apps.presentation"</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> |
Change to Batch Request
If it changes a lot it will be very long, difficult to understand so I will only make 1 BatchRequest. ※ Once you understand one, it’s easy to do a lot.
How to send a Batch Request
Batch Request should be sent as follows:
<dl> <dt> URL </dt> <dd> https://www.googleapis.com/batch </dd> <dt> Method </dt> <dd> POST </dd> <dt> Authorization Header </dt> <dd> Authorization: Bearer {ACCESS_TOKEN} </dd> <dt> Content-Type Header </dt> <dd> Content-Type: multipart / mixed; boundary = BOUNDARY </dd> <dt> Request Body </dt> <dd> Write each request here. Detail is recorded later. </dd> </dl>
Put Content-Type into multipart / mixed , I will tell you “contains many requests”. The boundary serves to separate each request. In this article we will specify the string “BOUNDARY”.
Request Body
Format as follows:
1 2 3 4 5 6 7 8 | --{BOUNDARY} Content-Type: application/http Content-ID: xxx Thông tin HTTP --{BOUNDARY}-- |
Assigning Content-ID to Content-Type represents the HTTP request and each corresponding request. Content-ID is fine as long as it doesn’t overlap. Also used when explaining the response.
The HTTP request information that is sent multiple times will be placed in the request body. When this will remove Authorization (even if there is left, it has no effect). It seems that the Authorization set in the Batch Request will be forcibly applied.
As shown in the picture
Try to send
Request Body will be saved in file to execute the specified file from curl.
1 2 3 4 5 6 7 8 | --BOUNDARY Content-Type: application/http Content-ID: 1 GET https://www.googleapis.com/drive/v3/files --BOUNDARY-- |
Request
1 2 | curl https://www.googleapis.com/batch -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: multipart/mixed; boundary=BOUNDARY" --data-binary @databinary.txt |
Response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | --batch_oysSsPRlgxc_AAP4j4WrTtE <span class="token header-name keyword">Content-Type:</span> application/http <span class="token header-name keyword">Content-ID:</span> response-1 <span class="token response-status">HTTP/1.1 <span class="token property">200 OK</span></span> <span class="token header-name keyword">Content-Type:</span> application/json; charset=UTF-8 <span class="token header-name keyword">Date:</span> Tue, 02 Feb 2016 21:40:32 GMT <span class="token header-name keyword">Expires:</span> Tue, 02 Feb 2016 21:40:32 GMT <span class="token header-name keyword">Cache-Control:</span> private, max-age=0 <span class="token header-name keyword">Content-Length:</span> 610 <span class="token application/json"> <span class="token punctuation">{</span> <span class="token string">"kind"</span> <span class="token punctuation">:</span> <span class="token string">"drive#fileList"</span> <span class="token punctuation">,</span> <span class="token string">"files"</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token string">"kind"</span> <span class="token punctuation">:</span> <span class="token string">"drive#file"</span> <span class="token punctuation">,</span> <span class="token string">"id"</span> <span class="token punctuation">:</span> <span class="token string">"1q5eHxwMySsmnAGV4x831txwWVZ6DDD1CmwCP4QcRBAs"</span> <span class="token punctuation">,</span> <span class="token string">"name"</span> <span class="token punctuation">:</span> <span class="token string">"Document 001"</span> <span class="token punctuation">,</span> <span class="token string">"mimeType"</span> <span class="token punctuation">:</span> <span class="token string">"application/vnd.google-apps.document"</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token string">"kind"</span> <span class="token punctuation">:</span> <span class="token string">"drive#file"</span> <span class="token punctuation">,</span> <span class="token string">"id"</span> <span class="token punctuation">:</span> <span class="token string">"1xXnBefD_jDcjZ-vEb50LO7xS01-AsHgdpKioQLegHrQ"</span> <span class="token punctuation">,</span> <span class="token string">"name"</span> <span class="token punctuation">:</span> <span class="token string">"Spread sheet 001"</span> <span class="token punctuation">,</span> <span class="token string">"mimeType"</span> <span class="token punctuation">:</span> <span class="token string">"application/vnd.google-apps.spreadsheet"</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token string">"kind"</span> <span class="token punctuation">:</span> <span class="token string">"drive#file"</span> <span class="token punctuation">,</span> <span class="token string">"id"</span> <span class="token punctuation">:</span> <span class="token string">"1NT8jIxP_NKbzgdJyQ4FPJTFaiAROmKZmIPq_TBS8wJY"</span> <span class="token punctuation">,</span> <span class="token string">"name"</span> <span class="token punctuation">:</span> <span class="token string">"Presentation 001"</span> <span class="token punctuation">,</span> <span class="token string">"mimeType"</span> <span class="token punctuation">:</span> <span class="token string">"application/vnd.google-apps.presentation"</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> <span class="token operator">--</span> batch_oysSsPRlgxc_AAP4j4WrTtE <span class="token operator">--</span> </span> |
Like when sending a request, BOUNDARY that Google set will also be set, so the client will separate by BOUNDARY and handle.
Content-ID will now have the form response-{Content-ID đã gửi khi request}
. With this value, it is possible to see which requests will respond. (might return the same number as the request which I thought should be more secure)
Expand (POST request side)
Even without Content-Length, it can run normally.
※ By the way, this request, if adding permission in the first one, it will omit the list of permissions in the second, the right added from the first request will not be shown in the result of the second permission list. BatchRequest does not guarantee order, so for requests that require an order, it will be NG.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | --BOUNDARY Content-Type: application/http content-id: 1 POST /drive/v3/files/17zoAl-wCvZdtvXLyW9lnlwODU77golDR_CcLK1mrgiE/permissions Content-Type: application/json; charset=UTF-8 { "emailAddress": " <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> ", "role": "writer", "type": "user" } --BOUNDARY Content-Type: application/http content-id: 2 GET /drive/v3/files/1428x4m9MSiMerQizvKXWbKhXh_a3Ye6lHhUl0HwjkEw/permissions --BOUNDARY-- |
Others
Request limit
Each type of API is also very diverse so please refer to the following sources. Drive は 100 https://developers.google.com/drive/v3/web/batch Calendar は 50 https://developers.google.com/google-apps/calendar/batch Directoy は 1000 https://developers.google .com / admin-sdk / directory / v1 / guides / batch
Note
Not all APIs can batch request.