title: Python Upload Files to Sharepoint using Shareplum
body: <p>I am using this much-shared code to try and upload a file to Sharepoint using Shareplum, into the Shared Documents folder.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import requests
  from shareplum import Office365
  
  # Set Login Info
  username = 'my.email@address.com'
  password = 'myverifiedapppassword'
  site_name = 'mysite'
  base_path = 'https://xxxxxxxx.sharepoint.com'
  doc_library = 'Shared%20Documents'
  file_name = "hellotest.txt" #when your file in the same directory
  
  # Obtain auth cookie
  authcookie = Office365(base_path, username=username, password=password).GetCookies()
  session = requests.Session()
  session.cookies = authcookie
  session.headers.update({'user-agent': 'python_bite/v1'})
  session.headers.update({'accept': 'application/json;odata=verbose'})
  
  session.headers.update({'X-RequestDigest': 'FormDigestValue'})
  response = session.post(url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)",
                           data="")
  session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})
  
  # Upload file
  with open(file_name, 'rb') as file_input:
      try:
          response = session.post(
              url=base_path + "/sites/" + site_name + f"/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='"
              + file_name + "',overwrite=true)",
  
              data=file_input)
          print("response: ", response.status_code) #it returns 200
          if response.status_code == '200':
              print("File uploaded successfully")
      except Exception as err:
          print("Something went wrong: " + str(err))
  
  print('File Uploaded Successfully')</code></pre>
  </div>
  </div>
  </p>
  <p>The problem is occuring wheen running the code....i am always getting a traceback and a keyerror as follows:</p>
  <p>Traceback (most recent call last):
  File &quot;S:\upload.py&quot;, line 22, in 
  session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})
  File &quot;C:\Python39\lib\site-packages\requests\structures.py&quot;, line 54, in <strong>getitem</strong>
  return self._store[key.lower()][1]
  KeyError: 'x-requestdigest'</p>
  <p>Something to do with x-requestdigest isnt working properly, in line 22, but i cannot figure out what.
  Any tips would be greatly appreciated!!!</p>
  <p>thanks</p>