Rest/API for File upload

SOLVE
Regular Contributor

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

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
HubSpot Moderator

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

HubSpot Developer Support
Reply
0 Upvotes
8 Replies 8
HubSpot Moderator

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

HubSpot Developer Support
Reply
0 Upvotes
Regular Contributor

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

 

Reply
0 Upvotes
HubSpot Moderator

Hi, @yorambaron.

 

Happy to clarify.

 

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

Isaac Takushi

HubSpot Developer Support
Reply
0 Upvotes
Regular Contributor

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

Reply
0 Upvotes
HubSpot Moderator

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

HubSpot Developer Support
Reply
0 Upvotes
Regular Contributor

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

 

Reply
0 Upvotes
HubSpot Moderator

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

HubSpot Developer Support
Reply
0 Upvotes
Highlighted
Regular Contributor

Thank you!