Hubspot File Post Api Not Working

$post_url = “https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=apikey”;
$upload_file = new CURLFile(‘https://dl.airtable.com/.attachments/4b3ae7d535258081f030e2552b667693/6a41a2b5/download3.png’,'multipart/form-data’);

$file_options = array(
“access” => “PUBLIC_INDEXABLE”,
“ttl” => “P3M”,
“overwrite” => true,
“duplicateValidationStrategy” => “NONE”,
“duplicateValidationScope” => “ENTIRE_PORTAL”

);

$post_data = array(
“file” => $upload_file,
“options” => json_encode($file_options),
“folderPath” => “/docs”
);
echo ‘<pre>’;
print_r($post_data);
echo ‘</pre>’;
$ch = curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_URL, $post_url);
@curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);

// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Log the response from HubSpot as needed.
print_r($response);
$image_url = json_decode($response ,true);
$url = $image_url[‘objects’][0][‘url’];
print_r($url);
curl_close($ch);

Hi @psama ,

What is the second param you pass to new CURLFile()?

Could you try setting it to ‘application/octet-stream’? So:

$upload_file = new CURLFile('https://dl.airtable.com/.attachments/4b3ae7d535258081f030e2552b667693/6a41a2b5/download3.png', 'application/octet-stream');

Hi @Teun
Thanks for your Response!
I have changed the content-type to ‘application/octet-stream’ but still I am getting the same issue.
Can you please check the below snapshot.

Thanks

Hi @psama ,

I think you need to switch your endpoint! Check this post.
Is your file URL set like this: https://dl.airtable.com/.attachments/4b3ae7d535258081f030e2552b667693/6a41a2b5/download3.png
?

Hi @Teun
Thanks for the information. let me try this.
Thank you

Hi @Teun
I have checked what I found is hubspot Make some updates in hubspot files api.
they didn’t released new updated hubspot post file api still,
if I am right,can you please check below snapshot and link

Thanks

Let me double check this with @dennisedson

Hi @Teun
yes sure please check it.
I Need to resolve this on urjent basis.
Thanks

Hi @psama ,

The post you shared is from november '20. I’m pretty sure the v3 files API is out of beta. Please look at the endpoints of Accounts Dashboard | HubSpot. I’ve answered a few questions about this endpoint and I am 100% positive it should work.

Hi @Teun
Txt file is uploading to hubspot filemamnager fine using the below code.
but I want to uload the jpg, png images url,I have set “https://dl.airtable.com/.attachments/cedb4c6f3caaa6c38e130f59512af7ac/5c1c3a7e/61018d8a8b8a5.jpeg”

Then code not working. can you please look into this
Thanks

<?php
$apikey = ‘my api key’;
$url = “https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=” . $apikey;
//$realpath = getcwd().“\\example.txt”;
$realpath = getcwd().“\\pankaj.txt”;
// cURL headers
$headers = array(“Content-Type:multipart/form-data”);

$upload_file = new CURLFile($realpath,‘application/octet-stream’, ‘pankaj.txt’);

$file_options = array(
“access” => “PUBLIC_INDEXABLE”,
“ttl” => “P3M”,
“overwrite” => false,
“duplicateValidationStrategy” => “NONE”,
“duplicateValidationScope” => “ENTIRE_PORTAL”
);

$post_data = array(
“file” => $upload_file,
“options” => json_encode($file_options),
“folderPath” => “/”, // /docs
);

$ch = curl_init();

$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_RETURNTRANSFER => true,
);

// cURL options
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
echo ‘<pre>’;
print_r($result);
echo ‘</pre>’;

if(!curl_errno($ch)) {
$info = curl_getinfo($ch);
if ($info[‘http_code’] == 200) {
$msg = “File uploaded successfully”;
echo $msg;
}
} else {
$errmsg = curl_error($ch);
echo $errmsg;
}

curl_close($ch);

?>

Hi @psama ,

Could you try setting the header to Content Type: text/plain
You should be able to upload any file type according to the docs.

Hi @Teun
Yes, I also tried with Content Type: text/plain . but still its not working.
can you please help me in this. I need to fix it on urjent basis.
Thanks

Hi @Teun
Upload Api Works fine Now
this is working code. but I want to upload multiple images at the same time . can you give Idea about that?

<?php
$apikey = ‘api key’;
$url = “https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=” . $apikey;

$url2 = “GrowthNatives: AI-Powered Marketing, CRM & Automation Agency”;
$url3 = “https://dl.airtable.com/.attachments/c6bd26c43079ae47a60cdbb2e5e2105f/65f13185/610178d018133.png”;
$pathinfo = pathinfo($url2);

$img= $pathinfo[‘filename’].‘.’.$pathinfo[‘extension’];
// Function to write image into file
file_put_contents($img,file_get_contents($url2));
echo “File downloaded!”;
$realpath = getcwd().“\\”. $img ;

// cURL headers
$headers = array(“Content-Type:multipart/form-data”);
//$upload_file = new CURLFile($realpath,‘application/octet-stream’,$img);
$upload_file = new CURLFile($realpath,‘application/octet-stream’,$img);
$file_options = array(
“access” => “PUBLIC_INDEXABLE”,
“ttl” => “P3M”,
“overwrite” => false,
“duplicateValidationStrategy” => “NONE”,
“duplicateValidationScope” => “ENTIRE_PORTAL”
);

$post_data = array(
“file” => $upload_file,
“options” => json_encode($file_options),
“folderPath” => “/docs”, // /docs
);
$ch = curl_init();
// cURL options
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
@curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$POST);
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Log the response from HubSpot as needed.
//print_r($response);
$image_url = json_decode($response ,true);
$hub_image_url = $image_url[‘objects’][0][‘url’];
print_r($url);
if(!curl_errno($ch)) {
$info = curl_getinfo($ch);
if ($info[‘http_code’] == 200) {
$msg = “File uploaded successfully”;
echo $msg;
}
} else {
$errmsg = curl_error($ch);
echo $errmsg;
}
curl_close($ch);
?>
Thanks