Manuel
Manuel

Reputation: 843

400 (An HTTP header that's mandatory for this request is not specified.) when trying to upload to Azure in c#

I am trying to upload a simple emtpy .txt to Azure blob storage from c# (currently just a proof of concept). I am doing this using a SAS token (which is working for all other cases I have tried sofar, like listing the containers or deleting a blob). But when trying to create a new blob I get the error that 400 (An HTTP header that's mandatory for this request is not specified.) I guess I have configured the request in the wrong way but I can not figure out the correct way. Can someone help? The Put Rest API call can be found here.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml.Linq;


namespace ConsoleProgram
{
    public class Class1
    {
        private static string URL = "https://example.blob.core.windows.net/";
        private static string sasToken = "sv=2019-12-12&ss=b&srt=sco&sp=rwdlacx&se=2021-02-02T15:52:32Z&st=2021-02-02T07:52:32Z&spr=https&sig=realSig";
        private static string command =  "";
        private static string commandType = "Put";


        static void PutBlob()
        {
            URL = "https://example.blob.core.windows.net/test/hello2.txt";
            command = "?";
            commandType = "Put";

        }
        static void ListContainers()
        {
            URL = "https://example.blob.core.windows.net/";
            command = "?comp=list&";
            commandType = "Get";
        }
        static void ListBlobs()
        {
            URL = "https://example.blob.core.windows.net/test";
            command = "?restype=container&comp=list&";
            commandType = "Get";
        }
        static void DeleteBlob()
        {
            URL = "https://example.blob.core.windows.net/test/hello2.txt";
            command = "?";
            commandType = "Delete";
        }

        static void Main(string[] args)
        {
            PutBlob();
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response;
            // List data response.
            if (commandType == "Get")
            {
                response = client.GetAsync(command + sasToken).Result;  // Blocking call! Program will wait here until a response is received or a timeout occurs.
            }
            else if (commandType == "Delete")
            {
                response = client.DeleteAsync(command + sasToken).Result;
            }
            else
            {
                // This is the Code that causes the problems

               var s=new StringContent("hello2.txt");
                response = client.PutAsync(command+sasToken,s).Result;
            }

                if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("---------------Positive Responce----------------");
                Console.WriteLine(response.ToString());
                // Parse the response body.
                var dataObjects = response.Content.ReadAsStringAsync().Result;  //Make sure to add a reference to System.Net.Http.Formatting.dll
            }
            else
            {
                Console.WriteLine("---------------Negative Responce----------------");
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }

            // Make any other calls using HttpClient here.

            // Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();
        }


    }
}

Upvotes: 8

Views: 11099

Answers (1)

Manuel
Manuel

Reputation: 843

Simple adding the line:

 client.DefaultRequestHeaders.Add("x-ms-blob-type", "BlockBlob");
              

fixed it for me.

Upvotes: 20

Related Questions