title: How to write to Sharepoint 2013 list using the Rest API
body: <p>I have been stuck for almost a week now, I want to write to a SharePoint list, usin the rest api they provide. The api looks like this, <a href="http://site/_api/lists" rel="nofollow">http://site/_api/lists</a>, and from here I can read and write depending on what I append to my url, I can read from the lists without any issues, but I have issues when I have to write.</p>
  
  <p>I am supposed to send in Content-Type, Accept, X-requestDigest headers, and post body when I write to list. My code</p>
  
  <pre><code>NSString *deviceToken = [self getDeviceTokenFromCoreData];
      NSString *postData =  [NSString stringWithFormat:@"{ \"__metadata\": { \"type\": \"SP.Data.TestListItem\" }, \"Title\": \"Test Title\" }"];
  
      NSData *methodBodyData = [postData dataUsingEncoding:NSUTF8StringEncoding];
      NSError *error;
      NSData *jsonString = [NSJSONSerialization JSONObjectWithData:methodBodyData options:0 error:&amp;error];
      NSString *acceptType = @"application/json;data=verbose";
      NSString *requestDigest = _requestDigest;
  
  
      NSURL *subscribeURL = [[NSURL alloc] initWithString:subscribeUrlString];
      NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:subscribeURL];
      [theRequest setHTTPMethod:@"POST"];
      [theRequest setHTTPBody:jsonString];
      [theRequest setValue:acceptType forHTTPHeaderField:@"Accept"];
      [theRequest setValue:acceptType forHTTPHeaderField:@"Content-Type"];
      [theRequest setValue:requestDigest forHTTPHeaderField:@"X-RequestDigest"];
  </code></pre>
  
  <p>This is where I construct my headers for the request. And this is how I handle the sending of the request</p>
  
  <pre><code>AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
  
      [operation setWillSendRequestForAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
  //        if (challenge.previousFailureCount == 0) {
  
          NSLog(@"%@", challenge.protectionSpace);
              NSURLCredential *creds = [NSURLCredential credentialWithUser:userName
                                                                  password:userPass
                                                               persistence:NSURLCredentialPersistenceForSession];
  
              [[challenge sender] useCredential:creds forAuthenticationChallenge:challenge];
          } else {
              [[challenge sender] cancelAuthenticationChallenge:challenge];
          }
      }];
  
      [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  
          //Handle Success
  
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  
         //Handle failure
      }];
  
      [operation start];
  }
  </code></pre>
  
  <p>This happens after I have authenticated to sharepoint, I have noticed during debugging the setWillSendRequestForAuthenticationChallengeBlock never gets called again, looks like I need to send the authentication information via the headers now, which is what I think the request digest is for, but that doesnt help cause I still don't get through.</p>
  
  <p>Error message I get from server is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;m:error 
      xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"&gt;
      &lt;m:code&gt;-2130575251, Microsoft.SharePoint.SPException&lt;/m:code&gt;
      &lt;m:message xml:lang="en-US"&gt;The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again.&lt;/m:message&gt;
  &lt;/m:error&gt;
  </code></pre>
  
  <p>Thanks in advance :)</p>