APIs & Integrations

Not applicable

Hubspot API File Manager Upload Issues using C#, but working when i used postman

Hi,
I am not able to upload an image using C# HttpWebRequest, but i tried with postman it’s working

i am using below code

string HubSpotkey = ConfigurationManager.AppSettings[“HubSpotkey”];
string file = @“C:\Users\sandeep.pothireddy\Downloads\HtmlToImage.png”;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(“http://api.hubapi.com/filemanager/api/v2/files?hapikey=” + HubSpotkey);

            string buffer = Convert.ToBase64String(System.IO.File.ReadAllBytes(file));

            string boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
            httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(buffer);
                streamWriter.Flush();
                streamWriter.Close();
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
0 Upvotes
1 Reply 1
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Hubspot API File Manager Upload Issues using C#, but working when i used postman

Hi @sandeeprdy.p,

I’m not fluent in C#, so I’m not the best resource to help troubleshoot your code. I was able to find a couple of examples of C# multipart form submissions online that might be helpful:

https://briangrinstead.com/blog/multipart-form-post-in-c/

0 Upvotes