Support Topics Documentation Slack YouTube Blog

Rest api:File content is absent in request body


(SnakeEyes) #1

Hello

I am trying to upload a file using cURL through php. Here is the code I am using

$fileVar = $_FILES["file"];
  $filename = $fileVar['name'];
    $filedata = $fileVar['tmp_name'];
    $filesize = $fileVar['size'];
$postfields = array("filedata" => "@$filedata");
$curl = curl_init();


curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, 'https://api.backendless.com/xxx/yyy/files/Documents/'.$_SESSION["username"].'/'.$sectionName.'/'.$filename.'/?overwrite=true');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_INFILESIZE , $filesize);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data')
);


$result = curl_exec($curl);


curl_close($curl);

But I keep getting
{“code”:6011,“message”:“Corrupted multipart request. File content is absent in request body”}

What is it that I am doing wrong here :frowning: ?
Thanks


(Mark Piller) #2

Hello,

Do you have a working curl request which does file upload? I recommend creating a working command first and then work off of it to transform to php.

Regards,
Mark


(SnakeEyes) #3

I have have working CURL in Php for login and adding data. When it comes to file upload, it is the body that throwing me off.

It is that part in cURL --form upload=@test.txt that I am not sure how it should be transfered to php.

I am using windows. I am pretty sure everything in curl and php is working, I am just not sure about the body part


(Mark Piller) #4

Sorry, unfortunately, I do not know how to transfer that to php. Perhaps you could post to some php-related forum?

Regards,
Mark


(SnakeEyes) #5

I FOUND IT!! :slight_smile: I spent hours and as soon as I replied to you , I found the reason. For people who might face the same issue, PHP 7 depricated the @ sign and now uses curlFile class instead.

Pheww. Quite a brick wall I ran against.
Thanks for your quick response though


(Richa Sugandhi) #6

Hello,
I am also using CurlFile But got error. Please help me.
$tmpPathName = ‘D:\xampp\tmp\phpC01E.tmp’;
$getType = ‘image/png’;
$cfile = new \CURLFile($tmpPathName, $getType, ‘image.png’);
$postfields = json_encode(array(“filedata” => $cfile));

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_URL, 'https://api.backendless.com/xxxx/yyyy/files/Restaurant/image.png/?overwrite=true');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: multipart/form-data')
        );
        $result = curl_exec($curl);
        curl_close($curl);

Error: “{“code”:8002,“message”:“Could not parse request with message: Missing boundary header, status code 400, headers POST /xxxx/yyyy/files/Restaurant/image.png/?overwrite=true”,“errorData”:{}}”