APIs & Integrations

Effecticore
Member | Partner
Member | Partner

Can´t get file upload via file API to work - please help!

SOLVE

Hi fellow Hubspotters, 

for some reason we can not get the file API to work. 

What we try to do is upload a file via the API using php code, but we keep getting errors or the file we try to upload via API never makes it to the /docs folder.
 
We use this stable version as per API documentation:
https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file?_ga=2.62397156.993641842.161796...

And this is the code we use (API key shortened here) - the code is currently only sitting in a snippet which is run when loading a page:

<?php

// This example uploads a local file named 'example_file.txt'
// to the /docs folder, without checking for duplicate files

$post_url = "https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=xxx";

$upload_file = new CURLFile('https://b2rmsw8i.myraidbox.de/wp-content/uploads/2021/04/EP-TEST_1217.pdf','application/octet-stream');

$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();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_URL, $post_url);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$response = @curl_exec($ch); //Log the response from HubSpot as needed.
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
@curl_close($ch);
echo $status_code . " " . $response;
?>

 
Any idea what the problem is?

Thanks for your help! Really appreciate it.
Best Lutz

0 Upvotes
1 Accepted solution
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Can´t get file upload via file API to work - please help!

SOLVE

@Effecticore - Your main offender is the URL.. should be https://api.hubapi.com/files/v3/files

 

Here's a sample code taken right out of my own app, so I know it works 😄

Unfortunately, it's using the OAuth token flow, but I think this will give you enough to go from

 

$post_url = "https://api.hubapi.com/files/v3/files";

      $upload_file = new \CURLFile($request->file('files')->getPathName(),'application/octet-stream', $request->file('files')->getClientOriginalName());

      $file_options = array(
          "access" => "PUBLIC_INDEXABLE",
          "overwrite" => false,
      );

      $post_data = array(
          "file" => $upload_file,
          "options" => json_encode($file_options),
          "folderPath" => "belch.io/uploads"
      );

      $authorization = "Authorization: Bearer " . $request->header('Token');


      $ch = curl_init();
      @curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization ));
      @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      @curl_setopt($ch, CURLOPT_POST, true);
      @curl_setopt($ch, CURLOPT_URL, $post_url);
      @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

      $response    = @curl_exec($ch);
      $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); 
      @curl_close($ch);
      //echo $status_code . " " . $response;

      $res = json_decode($response, true);
      $res['friendly_url'] = $res['url'];
      $return = [
        'objects' => [
          $res
        ]
      ];
      //$file = json_decode($response, true);
      return response()->json($return);

 

 

View solution in original post

2 Replies 2
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Can´t get file upload via file API to work - please help!

SOLVE

@Effecticore - Your main offender is the URL.. should be https://api.hubapi.com/files/v3/files

 

Here's a sample code taken right out of my own app, so I know it works 😄

Unfortunately, it's using the OAuth token flow, but I think this will give you enough to go from

 

$post_url = "https://api.hubapi.com/files/v3/files";

      $upload_file = new \CURLFile($request->file('files')->getPathName(),'application/octet-stream', $request->file('files')->getClientOriginalName());

      $file_options = array(
          "access" => "PUBLIC_INDEXABLE",
          "overwrite" => false,
      );

      $post_data = array(
          "file" => $upload_file,
          "options" => json_encode($file_options),
          "folderPath" => "belch.io/uploads"
      );

      $authorization = "Authorization: Bearer " . $request->header('Token');


      $ch = curl_init();
      @curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization ));
      @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      @curl_setopt($ch, CURLOPT_POST, true);
      @curl_setopt($ch, CURLOPT_URL, $post_url);
      @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

      $response    = @curl_exec($ch);
      $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); 
      @curl_close($ch);
      //echo $status_code . " " . $response;

      $res = json_decode($response, true);
      $res['friendly_url'] = $res['url'];
      $return = [
        'objects' => [
          $res
        ]
      ];
      //$file = json_decode($response, true);
      return response()->json($return);

 

 

dennisedson
HubSpot Product Team
HubSpot Product Team

Can´t get file upload via file API to work - please help!

SOLVE

@tjoyce , do you see any offenders in this code block?