Post asset upload from server api


#1

Hello,
I am trying to post my asset using the upload api.
This works fine when submitting a form from my browser.
But I am trying to do the form submit from a server api using client.PostAsync.
I applied the same fields and url to the server api as I did the browser html form.
Is there an example of this? I get a login page returned and no asset gets uploaded.
Thanks


#2

If you get a login screen than you either did not pass the API key or are not calling the page properly with the action. From experience, please check your parameters and make sure you pass them properly.


#3

Hi Nita,
I was missing some passed quotes. Working now. Code if it helps anyone else:

var boundary = $"----WebKitFormBoundary{DateTime.Now.Ticks.ToString(“x”)}";
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent(boundary))
{
formData.Headers.Remove(“Content-Type”);
formData.Headers.TryAddWithoutValidation(“Content-Type”, $“multipart/form-data; boundary={boundary}”);
var content = new StreamContent(stream);

                client.DefaultRequestHeaders.ExpectContinue = false;
            
                content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "\"filedata\"",
                    FileName = $"\"{fileName}\""
                };
                formData.Add(new StringContent("c.apiupload"), "\"fa\"");
                formData.Add(new StringContent(apikey), "\"api_key\""); 
                formData.Add(new StringContent(destfolderid), "\"destfolderid\""); 
                formData.Add(content);

                var returnedData = client.PostAsync(url, formData).Result;
                var textXml = returnedData.Content.ReadAsStringAsync().Result;

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(textXml);
                XmlNode row = doc.SelectSingleNode("//Response");
                return row?.SelectSingleNode("assetid")?.InnerText;                
            }