I'm following this https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest to try and delete a folder in a SharePoint library. I'm writing a c# console app but it's built using helpers from here (as I'll be using it within Dynamics CRM in the future) https://develop1.net/public/post/2014/12/29/SharePoint-Integration-Reloaded-e28093-Part-3.

It seems straightforward but if I send a null content in the post I get a 411 error 'Length Required'

        string odataQuery = $"_api/web/GetFolderByServerRelativeUrl('{folder}')";

        string digest = _spo.GetRequestDigest();
        Uri url = new Uri(String.Format("{0}/{1}", _spo.SiteUrl, odataQuery));

        var webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        webRequest.Headers.Add("X-RequestDigest", digest);
        webRequest.Headers.Add("IF-MATCH", "*");
        webRequest.Headers.Add("X-HTTP-Method", "DELETE");
            Console.WriteLine( url);
            byte[] result = HttpHelper.SendODataJsonRequest(url, "POST",null, webRequest, _spo);
            string response = Encoding.UTF8.GetString(result, 0, result.Length);
            return true;
        catch (Exception ex)
            return false;

//Code from the develop1 blog
    public static byte[] SendODataJsonRequest(Uri uri, String method, byte[] requestContent, HttpWebRequest clientHandler, SpoAuthUtility authUtility, Dictionary<string, string> headers = null)
        if (clientHandler.CookieContainer == null)
            clientHandler.CookieContainer = new CookieContainer();

        //trace("Get Cookie Container");
        CookieContainer cookieContainer = authUtility.GetCookieContainer(); // get the auth cookies from SPO after authenticating with Microsoft Online Services STS

        foreach (Cookie c in cookieContainer.GetCookies(uri))
            clientHandler.CookieContainer.Add(uri, c); // apppend SPO auth cookies to the request

        return SendHttpRequest(
            "application/json;odata=verbose;charset=utf-8", // the http content type for the JSON flavor of SP REST services 
   public static byte[] SendHttpRequest(Uri uri, String method, byte[] requestContent = null, string contentType = null, HttpWebRequest clientHandler = null, Dictionary<string, string> headers = null)
        HttpWebRequest request = clientHandler == null ? (HttpWebRequest)HttpWebRequest.Create(uri) : clientHandler;

        byte[] responseStream;

        request.Method = method;
        request.Accept = contentType;
        request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; // This must be here as you will receive 403 otherwise
        request.AllowAutoRedirect = false; // This is key, otherwise it will redirect to failed login SP page

        // append additional headers to the request
        if (headers != null)
            foreach (var header in headers)
                if (request.Headers.AllKeys.Contains(header.Key))

                request.Headers.Add(header.Key, header.Value);
                //if (tracer!= null)
                //tracer.Trace(header.Key.ToString() + ": " + header.Value.ToString());

        if (requestContent != null && (method == "POST" || method == "PUT" || method == "DELETE"))
            if (!string.IsNullOrEmpty(contentType))
                request.ContentType = contentType; // if the request has a body set the MIME type
                                                   // if (tracer != null)
                                                   //tracer.Trace("Content Type: " + contentType);

            string cont = ASCIIEncoding.ASCII.GetString(requestContent);

            request.ContentLength = requestContent.Length;

            using (Stream s = request.GetRequestStream())
                s.Write(requestContent, 0, requestContent.Length);


        // Not using Using here as you may still like to access the reponse outside of this method
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
        responseStream = Encoding.UTF8.GetBytes(sr.ReadToEnd());

        return responseStream;

1 Answer 1


Found some responses pointing to manually adding a content-length header of 0 for this webRequest.

Note that it's a 'restricted header' https://stackoverflow.com/questions/239725/cannot-set-some-http-headers-when-using-system-net-webrequest

so the code is

webRequest.ContentLength = 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.