-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
I'm working with batch requests, and adding custom request_id values. With a request_id value of length 28, the total length of the Content-ID value inside the < and > characters is 64.
Somehow, for >=64 characters, "\n " (including trailing space) gets added before the Content-ID value, but after the "Content-ID: ", resulting in a malformed Content-ID value which makes the round trip back (response takes the form Content-ID: response- <uuid+request_id> instead of Content-ID: <response-uuid+request_id>) and throws an exception (a BatchError for a malformed Content-ID value).
I've narrowed this down to this patch of googleapiclient/http.py starting at line 1234:
# encode the body: note that we can't use `as_string`, because
# it plays games with `From ` lines.
print(message) #no newline here
fp = StringIO()
g = Generator(fp, mangle_from_=False)
g.flatten(message, unixfrom=False)
body = fp.getvalue()
print(body,[body]) #newline appears here, printing a list shows newline as \n
This can be reproduced without requiring an authorised call, using the following test code:
from httplib2 import Http
from apiclient.discovery import build
directory = build('admin', 'directory_v1')
def errorTest(id_length):
req_id = "abcdefghijklmnopqrstuvwxyz0123456789"
try:
batch_dir = directory.new_batch_http_request()
batch_dir.add(directory.groups().list(), request_id=req_id[0:id_length])
batch_dir.execute(http=Http()) #BatchError, unprintable (separate issue)
except Exception as e:
print(e.reason)
raise
for i in [27,28]:
print(i,"############################################",i) #readability
try:
errorTest(i)
except AttributeError:
#only occurs if call not authorised; BatchError occurs first and isn't caught
pass