Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Chat API attachments:upload yields 502 with files >1Mb

Hi, I am using the HangoutsChat service of the PHP google/apiclient library to compose Google Chat messages with attachments. The attachment files themselves need to be referenced through attachmentUploadToken's acquired by POSTing them first through the media attachments:upload API, I figured out like this:

$request = new \Google\Service\HangoutsChat\UploadAttachmentRequest();
$request->setFilename('filename.ext');
$response = $hangoutschat_instance->media->upload(
    'space/xxx',
    $request,
    ['data' => 'binary contents', 'mimeType' => 'mime/type']
);
$uploadToken = $response->getAttachmentDataRef()->getAttachmentUploadToken();

This works except when files seem exceed "certain" size I get a 502 Bad Gateway response with HTML message "Error 502 (Server Error)!!1". This "certain" size seems to be between 100Kb and 1Mb, but certainly not the 200Mb limit stated by the API documentation. And it also isn't caused by filetype, because it happens with jpg, png, etc. Digging in the code tells me chunking the file is not possible through this API.

Can someone explain what is causing this error? 

TIA!

Solved Solved
0 1 143
1 ACCEPTED SOLUTION

I tracked down the issue myself, it is  due to Guzzle by default sending an Expect: 100-Continue header when having a POST body that exceeds 1Mb (see https://docs.guzzlephp.org/en/stable/request-options.html#expect). Google's API doesn't seem to like that header.

To fix this override the \Google\Client::createDefaultHttpClient() created client with your own one with the expect mechanism turned off, like this:

$google_client_instance->->setHttpClient(new \GuzzleHttp\Client([
'base_path' => \Google\Client::API_BASE_PATH,
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::EXPECT => false,
]));

View solution in original post

1 REPLY 1
Top Labels in this Space
Top Solution Authors