Jul 15, 2019 1:32 PM
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
Solved! Go to Solution.
Jul 19, 2019 5:31 PM
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 TakushiAssociate Certification Manager |
Jul 16, 2019 3:06 PM
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 TakushiAssociate Certification Manager |
Jul 17, 2019 1:43 PM
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
Jul 17, 2019 1:52 PM
Hi, @yorambaron.
Happy to clarify.
Per the article, you will want to use Content-Type: "multipart/form-data"
.
Isaac TakushiAssociate Certification Manager |
Jul 17, 2019 2:17 PM
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
Jul 18, 2019 5:25 PM
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 TakushiAssociate Certification Manager |
Jul 19, 2019 3:04 PM
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
Jul 19, 2019 5:31 PM
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 TakushiAssociate Certification Manager |
Sep 2, 2019 4:53 AM
Thank you!