title: Invalid Post: 400 - Bad Request with Python requests
body: <p>I'm using PoliteMail's ODATA RESTful API (Doc: <a href="http://kb.politemail.com/?p=1389" rel="nofollow noreferrer">PoliteMail API Entity Directory</a>)</p>
  
  <p>I'm trying to create a Contact inside of PoliteMail with <code>requests.post()</code>.</p>
  
  <p>Below are my steps</p>
  
  <h2>Load Libraries</h2>
  
  <pre><code>import requests
  import json
  
  # point to credentials
  import os
  import yaml
  myKeys = yaml.load(open(os.path.join(os.path.expanduser('~'), 'Documents\keyfile2.json')))
  </code></pre>
  
  <h2>Authentication Creds</h2>
  
  <pre><code>user = myKeys['PoliteMail'][1]['user']
  password = myKeys['PoliteMail'][1]['password']
  auth = requests.auth.HTTPBasicAuth(user, password)
  
  base_url = 'https://comm.microsoft.com/ssv3/odata/'
  entity = 'Lists'
  url = base_url+entity
  </code></pre>
  
  <h2>GET request</h2>
  
  <pre><code>r = requests.get(url+'(56)', auth=auth)
  print(r.status_code) # '200'
  </code></pre>
  
  <p>I can GET anything within this entity, but can't post.</p>
  
  <h2>POST request</h2>
  
  <p>Below is the structure <a href="http://kb.politemail.com/?p=1337" rel="nofollow noreferrer">given in the documentation</a></p>
  
  <pre><code>payload = {
  "ID":"0",
  "Name":"Test List",
  "Description":"Does this work",
  "IsNewsletter":"0",
  "Shared":False, 
  "CreationDate":"2014-11-19T23:00:00.000Z",
  "ActiveState":"1",
  "isAnonymous":False,
  "BusinessID":"0",
  "RegionID":"0"
  }
  </code></pre>
  
  <p>My request:</p>
  
  <pre><code>r = requests.post(url, data=json.dumps(payload),auth=auth)
  </code></pre>
  
  <p>It's output</p>
  
  <blockquote>
    <p>415: The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource."</p>
  </blockquote>
  
  <p>But when I add a header for content type:</p>
  
  <pre><code>r = requests.post(url, data=json.dumps(payload),auth=auth,
                       headers = {"Content-Type": "application/json"})
  </code></pre>
  
  <p>I'm told:</p>
  
  <blockquote>
    <p>400 "The request is invalid." An error has occurred.\r\n","type":"","stacktrace":""</p>
  </blockquote>
  
  <p>I've tried the following, to no avail:</p>
  
  <ul>
  <li><code>json=</code> instead of <code>data=</code></li>
  <li>removing quotes from num</li>
  <li>changing the ID value to something other than 0</li>
  <li>removing newline characters from the payload</li>
  </ul>
  
  <p>Any help is much appreciated!</p>
  
  <h2>Update</h2>
  
  <p>The errors are coming from the parsed text of the response below is the full response readout when I use <code>BeautifulSoup</code></p>
  
  <pre><code>{
    "odata.error":{
      "code":"","message":{
        "lang":"en-US","value":"The request is invalid."
      },"innererror":{
        "message":"contact : An error has occurred.\r\n","type":"","stacktrace":""
      }
    }
  }
  </code></pre>