Upload a file using api In C#

SOLVE
Abhimanyu
Contributor

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.

 

 

0 Upvotes
1 Accepted solution
Abhimanyu
Solution
Contributor

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);

View solution in original post

12 Replies 12
Abhimanyu
Solution
Contributor

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);

zwolfson
HubSpot Employee

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. 

quentin_lamamy
Key Advisor

I send a message to someone from the hubspot team, maybe he can help you


0 Upvotes
quentin_lamamy
Key Advisor

Why are you using the old api ?


0 Upvotes
Abhimanyu
Contributor

I am using this api: http://api.hubapi.com/files/v3/files?hapikey=key
Main issue is file.

0 Upvotes
quentin_lamamy
Key Advisor

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


0 Upvotes
Abhimanyu
Contributor

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?

0 Upvotes
quentin_lamamy
Key Advisor

Thanks for the answer, i suggest you to use the new endpoint documented here and use this php working code


0 Upvotes
Abhimanyu
Contributor

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);


0 Upvotes
quentin_lamamy
Key Advisor

@dennisedsonThanks for the ping

@AbhimanyuWhat is your actual code and what is the answer of the hubspot api ?


0 Upvotes
Abhimanyu
Contributor

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
}}

0 Upvotes
dennisedson
Community Manager

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?

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes