Invalid JSON payload received. Unable to parse number - google drive API
10:07 14 Nov 2024

Trying to upload file through the multipart upload API, as described in Google Drive developer documentation.

The following header was added:

Content-Type : multipart/related; boundary=foo_bar_baz
Content-Length :  calculated later
Authorization : token

Here is the request body:

--foo_bar_baz\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n{"name": "testname.txt","mimeType":"text/plain"}\r\n--foo_bar_baz\r\nContent-Type:text/plain\r\n\r\n1234142 test\r\n--foo_bar_baz--

Google Drive response is always:

{
    "error": {
        "code": 400,
        "message": "Invalid JSON payload received. Unable to parse number.\n--foo_bar_baz\\r\\nCon\n^",
        "errors": [
            {
                "message": "Invalid JSON payload received. Unable to parse number.\n--foo_bar_baz\\r\\nCon\n^",
                "domain": "global",
                "reason": "parseError"
            }
        ],
        "status": "INVALID_ARGUMENT"
    }
}

Here is the actual code in php using wordpress functions:

public function create_file2($file_name, $mime_type, $file_content, $parent) {

    $params = [
        'uploadType' => 'multipart',
        'supportsAllDrives' => 'true',
        'includeItemsFromAllDrives' => 'true'
    ];

    $metadata = [
        'name' => $file_name,
        'mimeType' => $mime_type
    ];
    
    // Füge parent hinzu, wenn vorhanden
    if (!empty($parent)) {
        $metadata['parents'] = [$parent];
    }
    $metadata_json = json_encode($metadata, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    $boundary = md5(time());

    $multipart_content = "--$boundary\r\n";
    $multipart_content .= "Content-Type: application/json; charset=UTF-8\r\n";
    $multipart_content .= "\r\n" . $metadata_json . "\r\n";
    $multipart_content .= "--" . $boundary . "\r\n";
    $multipart_content .= "Content-Type: $mime_type\r\n";
    $multipart_content .= "Content-Transfer-Encoding: base64\r\n";
    $multipart_content .= "\r\n" . base64_encode($file_content) . "\r\n";
    $multipart_content .= "--$boundary--\r\n";

    $url = KPR_gdrive_api_endpoint_files . '?' . http_build_query($params);

    $response = wp_remote_post($url, [
        'method' => 'POST',
        'headers' => [
            'Authorization' => 'Bearer ' . json_decode($this->token, true)['access_token'],
            'Content-Type' => 'multipart/related; boundary=' . $boundary,
            'Content-Length' => strlen($multipart_content)
        ],
        'body' => $multipart_content,
        'timeout' => 60
    ]);

    if (is_wp_error($response)) {
        error_log('create_file2 error: ' . $response->get_error_message());
        return false;
    }

    $response_body = json_decode(wp_remote_retrieve_body($response), true);

    
    if (isset($response_body['id'])) {
        return $response_body['id'];
    }

    error_log('create_file2 error: Unexpected response');
}

No matter how I tried to edit the request body, I get same error. Can I get any help here? I have been troubleshooting this issue for a week with no luck, any help is appreciated.

php wordpress google-drive-api google-api-client