Understand the structure of the Google API Batch Request

Tram Ho

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).


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.

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

Response

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:

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 Batch Request overview.png

Try to send

Request Body will be saved in file to execute the specified file from curl.

Request

Response

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.

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.

Share the news now

Source : Viblo