Google Drive API: Trouble parsing multipart upload payload

I’m hitting a wall with the Google Drive API multipart upload. I’ve been trying to upload a file but keep getting a 400 error. The API says it can’t parse the JSON payload.

Here’s what I’ve tried:

  • Set the right headers (Content-Type and Authorization)
  • Used a boundary in the multipart request
  • Formatted the request body with metadata and file content

But no matter what I do, I get this error:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unable to parse number.",
    "status": "INVALID_ARGUMENT"
  }
}

I’m using PHP with WordPress functions. Here’s a snippet of my code:

function upload_to_drive($filename, $content_type, $file_data, $folder_id = null) {
  $boundary = uniqid();
  $metadata = json_encode([
    'name' => $filename,
    'mimeType' => $content_type,
    'parents' => $folder_id ? [$folder_id] : []
  ]);

  $body = "--{$boundary}\r\n";
  $body .= "Content-Type: application/json\r\n\r\n";
  $body .= $metadata . "\r\n";
  $body .= "--{$boundary}\r\n";
  $body .= "Content-Type: {$content_type}\r\n\r\n";
  $body .= $file_data . "\r\n";
  $body .= "--{$boundary}--";

  // Send request to Google Drive API...
}

Any ideas what I’m doing wrong? I’m out of ideas and could really use some help!

I’ve encountered similar issues with the Google Drive API before, and I think I might know what’s going on here. The problem could be in how you’re constructing the multipart request body.

One thing that worked for me was ensuring there’s a blank line after each header and between the metadata and file content sections. Also, double-check that your metadata JSON is valid - even a small typo can cause parsing errors.

Have you tried using a tool like Postman to test your API requests? It can be really helpful for debugging these kinds of issues. You can construct the multipart request there and see exactly what’s being sent.

Another thing to watch out for is file encoding. Make sure your file_data isn’t being altered or encoded unexpectedly before it’s added to the request body.

If none of that helps, you might want to try logging the entire request body before sending it and compare it with the API documentation examples. Sometimes the smallest discrepancy can cause these frustrating errors.

Hope this helps you troubleshoot the problem!

I’ve dealt with this exact issue before, and it can be quite frustrating. Have you considered using Google’s official PHP client library? It handles a lot of the low-level details for you, including proper request formatting.

If you prefer to stick with your current approach, double-check your Content-Type header. It should be ‘multipart/related’, not just ‘multipart/form-data’. Also, ensure you’re setting the ‘uploadType’ query parameter to ‘multipart’ in your API request URL.

Another common pitfall is incorrect line endings. Try using \r\n consistently throughout your request body. And don’t forget to base64 encode your file data if it’s binary content.

Lastly, consider using cURL directly instead of WordPress functions for more control over the request. It might help isolate the issue.

hey man, ive had similar headaches with the drive API. one thing to check - are u escaping special chars in ur JSON? that can mess things up. also, try using PHP’s built-in http_build_query() for the metadata part. it handles url encoding better. Good luck!