1

I have a python azure function I'm trying to integrate with a C# backend. I'm trying to upload a file from an angular front end using a c# back end to post the data.

However, I'm getting a 401 error unauthorized. My function isnt anonymous level authentication and I'm attaching the keys to the headers, but is there something I'm missing that I need to include?

I've tried adding all the authentication to the headers and the form data headers but no luck.

public HttpResponseMessage UploadPartsTemplate()
        {
            MultipartFormDataContent form = new MultipartFormDataContent();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            string baseUrl = ConfigurationManager.AppSettings["PartsProjectAPIURL"];
            
            try
            {

                var fileBytes = Request.Content.ReadAsStreamAsync().Result;
                client.BaseAddress = new Uri(baseUrl);

                HttpContent content = new StringContent("");
                content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "file",
                    FileName = "template.xlsx"
                };

                content = new StreamContent(fileBytes);

                form.Add(content, "template.xlsx");
                form.Headers.Add("x-functions-key", ConfigurationManager.AppSettings["XFunctionsKey"]);
                form.Headers.Add("Ocp-Apim-Subscription-Key", ConfigurationManager.AppSettings["OcmAPISubscriptionKey"]);
                form.Headers.Add("Ocp-Apim-Trace", "true");
                form.Headers.Add("command", "validate");
                form.Headers.Add("code", ConfigurationManager.AppSettings["XFunctionsKey"]);
                client.DefaultRequestHeaders.Add("x-functions-key", ConfigurationManager.AppSettings["XFunctionsKey"]);
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ConfigurationManager.AppSettings["OcmAPISubscriptionKey"]);
                client.DefaultRequestHeaders.Add("Ocp-Apim-Trace", "true");
                client.DefaultRequestHeaders.Add("command", "validate");

                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

                var response = client.PostAsync(baseUrl, form).Result;

                return result;
            }
            catch(Exception ex)
            {
                throw;
            }
    }

And the POST Request in Postman works just fine. Any help would be appreciated!

POST Request 1

Post request 2

Thank you!

user1299379
  • 381
  • 2
  • 4
  • 13

0 Answers0