APIs & Integrations

yorambaron
Participant

Rest/API for File upload

SOLVE

Hello,

In the Hubspot documentation there is an example for File upload using Python code.

Is there a Rest API example for file upload?

 

Regards,

Yoram

0 Upvotes
1 Accepted solution
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

Rest/API for File upload

SOLVE

Hi, @yorambaron.

 

As I am not familiar with Java, I may not be of much further help, but what about trying the following approaches:

With OkHttp:

 

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\myphoto.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
Request request = new Request.Builder()
  .url("https://api.hubapi.com/filemanager/api/v2/files?hapikey=my_key")
  .post(body)
  .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
  .addHeader("Content-Type", "multipart/form-data")
  .build();

Response response = client.newCall(request).execute();

With Unirest:

 

HttpResponse<String> response = Unirest.post("https://api.hubapi.com/filemanager/api/v2/files?hapikey=my_key")
  .header("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
  .header("Content-Type", "multipart/form-data")
  .body("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\"myfile.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--")
  .asString();

 

Isaac Takushi

Associate Certification Manager

View solution in original post

0 Upvotes
9 Replies 9
IsaacTakushi
HubSpot Employee
HubSpot Employee

Rest/API for File upload

SOLVE

Hi, @yorambaron.

 

The CMS Files API, which this endpoint is part of, is a REST API. Are you referring to a JavaScript example?

 

If so, something like this just worked for me:

 

 

var data = new FormData();
data.append("files", "/Users/my/file/location.jpg");
data.append("file_names", "My File Name");

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.open("POST", "https://api.hubapi.com/filemanager/api/v2/files?hapikey=my_key");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.send(data);

Please let me know if I've misunderstood your request.

 

Isaac Takushi

Associate Certification Manager
SRitchot
Participant

Rest/API for File upload

SOLVE

Hi @IsaacTakushi,

I have been trying to send a replace file content request from my server (Node JS).
I want to update the content of a json file, but I am getting a 400 error:

Here's my code:

const FormData = require('form-data');
var hubspotHeaders = new Headers();
  // Tried the different headers
  // hubspotHeaders.set('content-type', 'multipart/form-data');
  // hubspotHeaders.set(
  //  'content-type',
  //  'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
  // );
  hubspotHeaders.set('content-type', 'application/x-www-form-urlencoded');
  hubspotHeaders.append('authorization', `Bearer AccessKey`);
 
const formData = new FormData();
formData.append('file', fs.createReadStream(filePath));
const requestOption: RequestInit = {
  method: 'PUT',
  headers: hubspotHeaders,
  body: formData,
};
const response = await fetch(`${_hubspotFileApiBaseUrl}/${fileId}`, requestOption);

Was hoping to get your help if possible,
Thank you
0 Upvotes
yorambaron
Participant

Rest/API for File upload

SOLVE

Hello Issac,

 

Thank you for your reply, I tried to write a simillar code in Java, I  received an error:

"415 Unsupported Media Type HTTP/1.1", I did set the value:

Content-type: "Application-Json" in the header.

 

Following is the Java code:

HttpPost post = new HttpPost("http://api.hubapi.com/filemanager/api/v2/files?hapikey=<value>");
JSONObject params = new JSONObject();
params.put("files", "<value>");
StringEntity stringEntity = new StringEntity(params.toString());
post.addHeader("Content-Type","application/json");
post.setEntity(new StringEntity(params.toString(), StandardCharsets.UTF_8));
HttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(post);

 

Any idea?

 

Regards,

Yoram

 

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

Rest/API for File upload

SOLVE

Hi, @yorambaron.

 

Happy to clarify.

 

Per the article, you will want to use Content-Type: "multipart/form-data".

Isaac Takushi

Associate Certification Manager
0 Upvotes
yorambaron
Participant

Rest/API for File upload

SOLVE

Hello Issac,

Thanks again for your help.

 

I changed the Content-type to "multipart/form-data".

I now receive the following error: "500 Internal Server Error HTTP/1.1".

 

Regards,

Yoram

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

Rest/API for File upload

SOLVE

Hi, @yorambaron.

 

I'm not proficient enough with Java to identify the issue in your code, but I'm seeing examples out there with both of these headers:

 

"content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
"Content-Type", "application/x-www-form-urlencoded"

Could you try with these?

Isaac Takushi

Associate Certification Manager
0 Upvotes
yorambaron
Participant

Rest/API for File upload

SOLVE

Hello Issac,

What you suggested  didn't work for me.

My understanding is that your API requires to load the entire file content into memory.

So far I haven't found a yet that it will work for me on a Java client code.

 

Regards,

Yoeam

 

0 Upvotes
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

Rest/API for File upload

SOLVE

Hi, @yorambaron.

 

As I am not familiar with Java, I may not be of much further help, but what about trying the following approaches:

With OkHttp:

 

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\myphoto.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
Request request = new Request.Builder()
  .url("https://api.hubapi.com/filemanager/api/v2/files?hapikey=my_key")
  .post(body)
  .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
  .addHeader("Content-Type", "multipart/form-data")
  .build();

Response response = client.newCall(request).execute();

With Unirest:

 

HttpResponse<String> response = Unirest.post("https://api.hubapi.com/filemanager/api/v2/files?hapikey=my_key")
  .header("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
  .header("Content-Type", "multipart/form-data")
  .body("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\"myfile.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--")
  .asString();

 

Isaac Takushi

Associate Certification Manager
0 Upvotes
yorambaron
Participant

Rest/API for File upload

SOLVE

Thank you!