3
replacement_requests = [
  Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: "{{name}}", replace_text: "Joe"),
  Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: "{{age}}", replace_text: "34"),
  Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: "{{address}}", replace_text: "Westwood"),
]

batch_request = Google::Apis::DocsV1::BatchUpdateDocumentRequest.new(requests: replacement_requests)

Given the above code, when I pass this BatcUpdateDocumentRequest instance into my service.batch_update_document function, I receive a 400 bad request. This seems to be related to the way the batch request is being serialized.

To illustrate, if we call batch_request.to_json we receive the following:

"{\"requests\":[{},{},{}]}"

This tells me that something is going wrong during serialization, however my code seems rather canonical.

Any thoughts on why my requests are failing to be serialized?

1 Answers1

6
  • You want to use the replaceAllText request using google-api-client with ruby.
  • You have already been able to put and get values for Google Document using Google Docs API.

If my understanding is correct, how about this modification? In your script, the created request body is {"requests":[{},{},{}]}. By this, the error occurs. Please modify the script as follows.

Modification points:

  • Use Google::Apis::DocsV1::SubstringMatchCriteria for contains_text of Google::Apis::DocsV1::ReplaceAllTextRequest
  • Use Google::Apis::DocsV1::Request for Google::Apis::DocsV1::ReplaceAllTextRequest.

By above modification, the request body is created.

Modified script:

text1 = Google::Apis::DocsV1::SubstringMatchCriteria.new(text: "{{name}}")
text2 = Google::Apis::DocsV1::SubstringMatchCriteria.new(text: "{{age}}")
text3 = Google::Apis::DocsV1::SubstringMatchCriteria.new(text: "{{address}}")

req1 = Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: text1 , replace_text: "Joe")
req2 = Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: text2, replace_text: "34")
req3 = Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: text3, replace_text: "Westwood")

replacement_requests = [
    Google::Apis::DocsV1::Request.new(replace_all_text: req1),
    Google::Apis::DocsV1::Request.new(replace_all_text: req2),
    Google::Apis::DocsV1::Request.new(replace_all_text: req3)
]

batch_request = Google::Apis::DocsV1::BatchUpdateDocumentRequest.new(requests: replacement_requests)

# result = service.batch_update_document(document_id, batch_request)  # When you request with "batch_request", you can use this.

Request body:

When above script is run, the following request body is created.

{"requests":[
  {"replaceAllText":{"containsText":{"text":"{{name}}"},"replaceText":"Joe"}},
  {"replaceAllText":{"containsText":{"text":"{{age}}"},"replaceText":"34"}},
  {"replaceAllText":{"containsText":{"text":"{{address}}"},"replaceText":"Westwood"}}
]}

Note:

  • When the error related to the authorization occurs, please confirm the scopes and whether Docs API has been enabled.

References:

If this didn't work, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This did it. Thank you so much. – Joe Letizia Sep 20 '19 at 14:07
  • @Joe Letizia Thank you for replying. I'm glad your issue was resolved. – Tanaike Sep 20 '19 at 23:36
  • 2
    Thanks for this answer for v0.9. This is correct! Coming from the `google-api-ruby-client` v0.8, I can tell it was way easier, all it took was creating the hash `requests` and jsonifying it. All these classes you need to know of, it's so over-engineered as if this was Java-land – mrnovalles Oct 23 '19 at 14:56