Skip to content

Content-ID header over 64 characters gets an extra newline; breaks batch requests #164

@ShadowLNC

Description

@ShadowLNC

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

Metadata

Metadata

Assignees

Labels

🚨This issue needs some love.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions