Home > Software engineering >  upload CSV file to google drive with curl fails: "Bad content type. Please use multipart."
upload CSV file to google drive with curl fails: "Bad content type. Please use multipart."

Time:11-21

I'm trying to upload a CSV file to google drive and I'm getting the error described in the subject of this post.

this is my code:

I think my error is in the content generation. I've seen some other posts but I can't resolve it yet.

this is the code

public function uploadFileToDrive($token, $fileContent){
        echo "Iniciando subida de archivo a drive .... \n";
        
        try {
            $apiUrl = 'https://www.googleapis.com/';
            $ch1 = curl_init();

           
            /* MEtODO 1 */
            $mime_type = 'text/csv';
            $data = '
            --section_divider
            Content-Type: application/json; charset=UTF-8
            {
                "name": "test.xlsx",
            }

            --section_divider
            Content-Type: '.$mime_type.',
            '.$fileContent.'
            --section_divider--
            ';

           
            //print_r($body);die();
            curl_setopt($ch1, CURLOPT_URL, $apiUrl . 'upload/drive/v3/files?uploadType=multipart');
            curl_setopt($ch1, CURLOPT_POST, 1);
            curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

            curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: multipart/related;section_divider', 'Authorization: Bearer' . $token) );

            $response = curl_exec($ch1);
            if ($response === false) {
                echo 'Curl error: ' . curl_error($ch1);
            } else {
                echo "Operation completed without any errors \n";
                $output = $response;
            }
            curl_close($ch1);
            var_dump($output);die();
            return $output;

        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
        }

    }

I don't know what I'm doing wrong

I hope you can give some advice
thanks

The expected results. See the CSV file uploaded on google drive.

See in the console a response indicating that all it's done

CodePudding user response:

I thought that in your script, $data and Content-Type are required to be modified. So, how about the following modification?

From:

$data = '
--section_divider
Content-Type: application/json; charset=UTF-8
{
    "name": "test.xlsx",
}

--section_divider
Content-Type: '.$mime_type.',
'.$fileContent.'
--section_divider--
';


//print_r($body);die();
curl_setopt($ch1, CURLOPT_URL, $apiUrl . 'upload/drive/v3/files?uploadType=multipart');
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: multipart/related;section_divider', 'Authorization: Bearer' . $token) );

To:

$data = '
--section_divider
Content-Type: application/json; charset=UTF-8

{"name": "test.csv", "mimeType": "text/csv"}

--section_divider
Content-Type: '.$mime_type.',

'.$fileContent.'

--section_divider--';

//print_r($body);die();
curl_setopt($ch1, CURLOPT_URL, $apiUrl . 'upload/drive/v3/files?uploadType=multipart');
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: multipart/related;boundary=section_divider', 'Authorization: Bearer ' . $token) );
  • By this modification, the following file metadata of the uploaded file is returned.

      {
       "kind": "drive#file",
       "id": "###",
       "name": "test.csv",
       "mimeType": "text/csv"
      }
    

Note:

  • In your script, although you want to upload a CSV file, the filename is test.xlsx. And also, the mimeType is not set. By this, the mimeType of the uploaded file is automatically set as application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. When you want to upload a CSV file as the CSV data, please set the mimeType of text/csv.

  • If you want to upload the CSV data as Google Spreadsheet, please set the mimeType as "mimeType": "application/vnd.google-apps.spreadsheet" instead of "mimeType": "text/csv".

  • In your script, 'Authorization: Bearer' . $token is used. In this case, if $token is ya29.###, it is required to insert a space like 'Authorization: Bearer ' . $token. In my modification, 'Authorization: Bearer ' . $token is used. Please be careful about this.

Reference:

  • Google

  • Related