title: Need to add a folder to a document library in SharePoint 2013 using REST - SharePoint Hosted app
body: <p>I'm trying to add a folder to a document library using REST in a SharePoint 2013 SharePoint hosted app.  It is hosted in an Office 365 developer site.  I'm following the guidelines in MSDN <a href="https://msdn.microsoft.com/en-us/library/dn292553.aspx" rel="nofollow">here</a> as well as using the excellent samples on <a href="http://dev.office.com" rel="nofollow">dev.office.com</a> (especially the REST Helper).</p>
  
  <p>While capturing the request during debugging I can't see any problems with my request compared to the documentation and samples.  I haven't had issues with any GET requests either.</p>
  
  <p>Here is my request:</p>
  
  <pre><code>var datatext = "{'__metadata': {'type': 'SP.Folder'}, 'ServerRelativeUrl': '" + serverRelativeUrl + foldername + "'}";
  
  $.ajax({
      url : $appweburl + "/_api/web/folders",
      type: "POST",
      data: datatext,
      headers: {
          "accept": "application/json;odata=verbose",
          "X-RequestDigest": $("#__REQUESTDIGEST").val(),
          "content-type": "application/json;odata=verbose",
          "content-length": datatext.length
      },
      success: function (data) {
          success(data);
      },
      error: function (data) {
          failure(data);
      }
  });
  </code></pre>
  
  <p>This generates an error "value does not fall within the expected range"</p>
  
  <p>So this uses "data" instead of "body" because "body" was always generating a JSON Reader error.  All of the samples indicate "body" but I found other sources that show this key should be "data" in a POST like this, and it resolves the JSON reader error.  I ensure that ServerRelativeUrl leads with a "/" as well as filename.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p><strong>EDIT:</strong> To follow up on this, I was successful using JSOM only.  It turns out that you must add a ListItem to the list and specify the content type to be a folder to achieve this.  It works, but it makes me wonder if you have to do something similar in REST - that is rather than use the folder endpoints, do it like adding a listitem to a list.  I have not confirmed it.</p>