Migrate from HTTP batch requests

HTTP batch requests, a feature that lets you send multiple API calls in a single HTTP request, is deprecated for the Google Docs API as of July 19, 2021. Requests sent as part of an HTTP batch request will fail after July 19, 2022.

To migrate away from HTTP batch requests, we recommend that you execute the individual requests separately.

Alternative batching methods

If the batch requests you want to make are within a single document, you might be able to use the documents.batchUpdate method instead.

Unlike HTTP batch requests, this batch method counts as a single API call for quota usage, and guarantees all updates in the batch will be applied together atomically.

Migration example

The following code sample shows an HTTP batch request that needs to be updated to individual executions.

C#

// Create a batch to add the requests to:
var batch = new BatchRequest(docsService);

// Build some regular Docs API requests, but don't execute them yet:
DocumentsResource.GetRequest request1 =
    docsService.Documents.Get(documentId1);

DocumentsResource.GetRequest request2 =
    docsService.Documents.Get(documentId2);

// Queue up the requests to be executed as part of the batch, providing
// callbacks to run when the requests finish.
batch.Queue<Document>(
    request1,
    (response, error, i, message) => {
      if (error != null) {
        // Do something in the event of failure:
        Console.WriteLine(error);
      } else {
        // Do something with the result:
        Console.WriteLine(response);
      }
    });

batch.Queue<Document>(
    request2,
    (response, error, i, message) => {
      if (error != null) {
        // Do something in the event of failure:
        Console.WriteLine(error);
      } else {
        // Do something with the result:
        Console.WriteLine(response);
      }
    });

// Execute all queued requests as a single batch.
await batch.ExecuteAsync();

Java

// Create a batch to add the requests to:
BatchRequest batch = docsService.batch();

// Build some regular Docs API requests, but don't execute them yet:
Docs.Documents.Get request1 = docsService.documents().get(documentId1);
Docs.Documents.Get request2 = docsService.documents().get(documentId2);

// Queue up the requests to be executed as part of the batch, providing
// callbacks to run when the requests finish.
request1.queue(
    batch,
    new JsonBatchCallback<Document>() {
      public void onSuccess(Document response, HttpHeaders responseHeaders) {
          // Do something with the result:
          System.out.println(response);
        }
        public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
          // Do something in the event of failure:
          System.out.println(e);
        }
    });

request2.queue(
    batch,
    new JsonBatchCallback<Document>() {
      public void onSuccess(Document response, HttpHeaders responseHeaders) {
          // Do something with the result:
          System.out.println(response);
        }
        public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
          // Do something in the event of failure:
          System.out.println(e);
        }
    });

// Execute all queued requests as a single batch.
batch.execute();

Python

# Create a batch to add the requests to:
batch = docsService.new_batch_http_request()

# Build some regular Docs API requests, but don't execute them yet:
request1 = docsService.documents().get(documentId=document_id1)
request2 = docsService.documents().get(documentId=document_id2)

# Queue up the requests to be executed as part of the batch, providing
# callbacks to run when the requests finish.
def callback(request_id, response, exception):
  if exception is not None:
    pprint(exception)
  else:
    pprint(response)

batch.add(request1, callback)
batch.add(request2, callback)

# Execute all the queued requests as a single batch.
batch.execute()

Ruby

# Create a batch to run requests in.
docsService.batch do |batch|
  # Queue up requests to be run as part of the batch. The block will be
  # executed when the request completes.
  batch.get_document(document_id1) do |res, err|
    if err != nil then
      puts "#{err}"
    else
      puts res.to_json
    end
  end

  batch.get_document(document_id2) do |res, err|
    if err != nil then
      puts "#{err}"
    else
      puts res.to_json
  end
end
# All batch requests queued as part of the block will be executed
# automatically.

The following code sample shows the previously batched requests as individual executions.

C#

DocumentsResource.GetRequest request1 =
    docsService.Documents.Get(documentId1);

try {
  Document response = await request1.ExecuteAsync();
  // Do something with the result:
  Console.WriteLine(response);
} catch (GoogleApiException error) {
  // Do something in the event of failure:
  Console.WriteLine(response);
}

DocumentsResource.GetRequest request2 =
    docsService.Documents.Get(documentId2);
request2.ValueInputOption =
    DocumentsResource.GetRequest.ValueInputOptionEnum.RAW;

try {
  Document response = await request2.ExecuteAsync();
  // Do something with the result:
  Console.WriteLine(response);
} catch (GoogleApiException error) {
  // Do something in the event of failure:
  Console.WriteLine(response);
}

Java

Docs.Documents.Get request1 = docsService.documents().get(documentId1);

try {
  Document response = request1.execute();
  // Do something with the result:
  System.out.println(response);
} catch (IOException e) {
  // Do something in the event of failure:
  System.out.println(e);
}

Docs.Documents.Get request2 = docsService.documents().get(documentId2);

try {
  Document response = request2.execute();
  // Do something with the result:
  System.out.println(response);
} catch (IOException e) {
  // Do something in the event of failure:
  System.out.println(e);
}

Python

# Build some regular Docs API requests, but don't execute them yet:
request1 = docsService.documents().get(documentId=document_id1)

try:
  response = request1.execute()
  # Do something with the result:
  pprint(response)
except HttpError as exception:
  # Do something in the event of failure:
  pprint(exception)

request2 = docsService.documents().get(documentId=document_id2)

try:
  response = request2.execute()
  # Do something with the result:
  pprint(response)
except HttpError as exception:
  # Do something in the event of failure:
  pprint(exception)

Ruby

docsService.get_document(document_id1, range1, request_body1) do |res, err|
  if err != nil then
    puts "#{err}"
  else
    puts res.to_json
  end
end

docsService.get_document(document_id2) do |res, err|
  if err != nil then
    puts "#{err}"
  else
    puts res.to_json
end