8

I am trying to post data to a SharePoint 2013 REST API using Python. I am able to successfully acquire the required X-RequestDigest using the method from this post.

I have already tried solutions from here and here.

I can extract the required token every time, but I get this response from the server when I try to use it:

<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>-1, System.Collections.Generic.KeyNotFoundException</m:code>
<m:message xml:lang="en-US">The given key was not present in the dictionary.</m:message>
<m:innererror><m:message>The given key was not present in the dictionary.</m:message>
<m:type>System.Collections.Generic.KeyNotFoundException</m:type>
<m:stacktrace>   
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)&#xD;
   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.ReadRequestODataObject(ServerStub serverStub, Object entity)&#xD;
   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.InvokeMethodWithRequestODataEntry(Object target, ServerStub serverStub, String methodName)&#xD;
   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.TryAddEntity(Object entity, ServerStub serverStub, Object&amp; newEntity)&#xD;
   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.Process()&#xD;
   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.ProcessRequest()&#xD;
   at Microsoft.SharePoint.Client.Rest.RestService.ProcessQuery(Stream inputStream, IList`1 pendingDisposableContainer)</m:stacktrace></m:innererror></m:error>

I use the following code:

# First, get security token
token_url = "https://example.com/_api/contextinfo"
tr = requests.post(token_url, auth=HttpNtlmAuth(creds[0], creds[1]),verify=False)
tokenRoot = ElementTree.fromstring(tr.content)

# Since the namespace will not be parsed, it is hardcoded into the string
token = tokenRoot.find("{http://schemas.microsoft.com/ado/2007/08/dataservices}FormDigestValue").text
print(token)

headers = {'X-RequestDigest': token}
post_url = "https://example.com/_api/web/lists/getbyTitle('listname')/items"

payload = {'__metadata': {'type': 'SP.Data.ListName'},'Title': 'test', 'date_observation': '2015-08-10T22:00:00Z'}

r = requests.post(post_url, data=json.dumps(payload), auth=HttpNtlmAuth(creds[0], creds[1]), headers = headers, verify=False)
print(r.status_code)

Any ideas whatsoever?

4

I came across this error when there was a malformed field in a list. I found it by using the Developer Dashboard and just making the api call from there.

It showed me that there was a lookup list that it could not open, and the lookup field had a render error.

Maybe you are in a similar situation?

2
  • This explained my issue, kind of. One of the document libraries in my site collection had a field that caused this error when returned from the REST api. Aug 12 at 23:38
  • My solution was to use the $Select filter to limit the number of fields to avoid the "malformed field" aka the field that caused SharePoint to blow up when we requested this field. /Items?%24filter=FileRef+eq+%27%2Fsites%2Fspecial-characters%2FLists%2Fcustom_list%2Ffolder%2Fsubfolder%27&%24top=1 throws the error, until changed to: to /Items?%24filter=FileRef+eq+%27%2Fsites%2Fspecial-characters%2FLists%2Fcustom_list%2Ffolder%2Fsubfolder%27&%24top=1&%24select=id%2CGUID Aug 12 at 23:39
3

Question resolved. The error message is completely misleading. It has nothing to do with the token - the problem lies within the strict POST message format SharePoint is requiring. If you use the following headers for your POST request, everything works fine.

headers = {'X-RequestDigest': token,
           'Accept': 'application/json; odata=verbose',
           'Content-Type': 'application/json; odata=verbose',
           'Content-Length': len(json.dumps(payload))}
1
  • I've also encountered this issue when I was using "POST" and should have been using "GET".
    – Paul Rowe
    May 17 '19 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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