We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
Nov 30, 2020
7:49 AM
- last edited on
Nov 30, 2020
4:00 PM
by
dennisedson
Hi
I want to use this endpoint to upload file to hub spot in c#.
http://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=dem
Documnet URL: https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file
I am not able to make request model for that any help will be appriciated.
Thank you.
Solved! Go to Solution.
Dec 5, 2020 12:40 PM
Hi finally I am to to upload bytes to hubspot in C#
Working code
var file = File.ReadAllBytes("C:\\Users\\abhimanyu.kumar\\Downloads\\1.png");
var request = new RestRequest(Method.POST);
var client = new RestClient("http://api.hubapi.com/files/v3/files?hapikey=key");
request.AddFile("file", file, "FileName.png", "application/octet-stream");
request.AddParameter("folderPath", "/path");
request.AddParameter("options", properties);
IRestResponse response = client.Execute(request);
Dec 5, 2020 12:40 PM
Hi finally I am to to upload bytes to hubspot in C#
Working code
var file = File.ReadAllBytes("C:\\Users\\abhimanyu.kumar\\Downloads\\1.png");
var request = new RestRequest(Method.POST);
var client = new RestClient("http://api.hubapi.com/files/v3/files?hapikey=key");
request.AddFile("file", file, "FileName.png", "application/octet-stream");
request.AddParameter("folderPath", "/path");
request.AddParameter("options", properties);
IRestResponse response = client.Execute(request);
Dec 4, 2020 9:57 AM
Hi @Abhimanyu
I think the issue is that the overall request should use the content type of multipart/form-data. Then the file itself should have applicaiton/octet-stream.
Dec 4, 2020 5:50 AM
I send a message to someone from the hubspot team, maybe he can help you
Dec 3, 2020 5:17 AM
Why are you using the old api ?
Dec 3, 2020 11:12 PM
I am using this api: http://api.hubapi.com/files/v3/files?hapikey=key
Main issue is file.
Dec 2, 2020 8:15 AM
Maybe the file header is wrong but if you found a way to do it it's a good new. C# is not my main programming lang, i use to learn at university but now i'm most of the time doing nodeJs and php
Dec 2, 2020 3:37 PM - edited Dec 2, 2020 3:39 PM
I am sending the file header 'application/octet-stream' as mention in the document https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file?_ga=2.255046920.1059212467.1604...
And are you sending the path of the file or bytes?
Dec 1, 2020 6:05 AM - edited Jan 21, 2021 12:41 PM
Thanks for the answer, i suggest you to use the new endpoint documented here and use this php working code
Dec 2, 2020 6:02 AM
Hi @quentin_lamamy @dennisedson
this is working code but intead of file path "C:\\Users\\abhimanyu.kumar\\Downloads\\1.png" I wasnt to send bytes like
var file = new ByteArrayContent(File.ReadAllBytes("C:\\Users\\abhimanyu.kumar\\Downloads\\1.png"));
file.Headers.Add("Content-Type", "application/octet-stream");
And I am trying it and response I am getting is unspupported type
What is supported type for file?
Working Code:
var options1 = new { access = "PUBLIC_INDEXABLE" };
var properties1 = JsonConvert.SerializeObject(options1);
var request1 = new RestRequest(Method.POST);
var client = new RestClient("http://api.hubapi.com/files/v3/files?hapikey=demo");
request1.AddFile("file", "C:\\Users\\abhimanyu.kumar\\Downloads\\1.png");
request1.AddParameter("folderId", "381726034078");
request1.AddParameter("options", properties1);
IRestResponse response1 = client.Execute(request1);
Nov 30, 2020 7:34 PM - edited Jan 21, 2021 12:41 PM
@dennisedsonThanks for the ping
@AbhimanyuWhat is your actual code and what is the answer of the hubspot api ?
Dec 1, 2020 5:32 AM
Hi @quentin_lamamy
Here's my code
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "http://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=demo"))
{
var multipartContent = new MultipartFormDataContent();
var options = new
{
options = new { access = "PUBLIC_INDEXABLE" }
};
var properties = JsonConvert.SerializeObject(options);
var file1 = new ByteArrayContent(File.ReadAllBytes("C:\\Users\\abhimanyu.kumar\\Downloads\\1.png"));
file1.Headers.Add("Content-Type", "multipart/form-data");
multipartContent.Add(file1, "+", Path.GetFileName("1.png"));
multipartContent.Add(new StringContent(properties));
multipartContent.Add(new StringContent("/Abhimanyu_Test"), "folderPath");
request.Content = multipartContent;
var response = await httpClient.SendAsync(request);
}
And Response is
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Date: Tue, 01 Dec 2020 10:27:29 GMT
Connection: keep-alive
Set-Cookie: __cfduid=d439fa73df3a0b3f3f5d5b3b862563a331606818449; expires=Thu, 31-Dec-20 10:27:29 GMT; path=/; domain=.hubapi.com; HttpOnly; SameSite=Lax
X-Trace: 2B3B778C5B16B2FEEB75D4A27E9BE1A3C8C64CF3A1000000000000000000
X-HubSpot-RateLimit-Daily: 500000
X-HubSpot-RateLimit-Daily-Remaining: 499907
X-HubSpot-RateLimit-Interval-Milliseconds: 10000
X-HubSpot-RateLimit-Remaining: 99
X-HubSpot-RateLimit-Max: 100
X-HubSpot-RateLimit-Secondly: 10
X-HubSpot-RateLimit-Secondly-Remaining: 9
Access-Control-Allow-Credentials: false
CF-Cache-Status: DYNAMIC
cf-request-id: 06bf70e0e90000ab04850e7000000001
CF-RAY: 5fac1dae3824ab04-MAA
Cache-Control: no-store, must-revalidate, no-cache
Server: cloudflare
Content-Type: text/html; charset=iso-8859-1
Content-Length: 267
}}
Nov 30, 2020 1:33 PM
Hey @Abhimanyu
I know there is a wrapper that allows you to upload a file to an engagement. Maybe looking at that you can figure out how to compose this.
@quentin_lamamy , I know you usually work with the CRM, but have you tried uploading to the file manager?
![]() | Make sure to subscribe to our YouTube channel where you can find the HubSpot Community Developer Show |