Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Post: 400 - Bad Request with Python requests

I'm using PoliteMail's ODATA RESTful API (Doc: PoliteMail API Entity Directory)

I'm trying to create a Contact inside of PoliteMail with requests.post().

Below are my steps

Load Libraries

import requests
import json

# point to credentials
import os
import yaml
myKeys = yaml.load(open(os.path.join(os.path.expanduser('~'), 'Documents\keyfile2.json')))

Authentication Creds

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

GET request

r = requests.get(url+'(56)', auth=auth)
print(r.status_code) # '200'

I can GET anything within this entity, but can't post.

POST request

Below is the structure given in the documentation

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"
}

My request:

r = requests.post(url, data=json.dumps(payload),auth=auth)

It's output

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."

But when I add a header for content type:

r = requests.post(url, data=json.dumps(payload),auth=auth,
                     headers = {"Content-Type": "application/json"})

I'm told:

400 "The request is invalid." An error has occurred.\r\n","type":"","stacktrace":""

I've tried the following, to no avail:

  • json= instead of data=
  • removing quotes from num
  • changing the ID value to something other than 0
  • removing newline characters from the payload

Any help is much appreciated!

Update

The errors are coming from the parsed text of the response below is the full response readout when I use BeautifulSoup

{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"The request is invalid."
    },"innererror":{
      "message":"contact : An error has occurred.\r\n","type":"","stacktrace":""
    }
  }
}
like image 749
Anders Swanson Avatar asked May 04 '26 16:05

Anders Swanson


2 Answers

I was able to create a list with your sample by fixing the capitalization of False to false and isAnonymous to IsAnonymous in the 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"
}

You also mentioned creating a Contact at the beginning of your post. I was able to successfully create a Contact with the following.

{
      "DisplayName": "Test Test",
      "FirstName": "Test",
      "LastName": "Test",
      "Email": "[email protected]",
      "ActiveState": true,
      "CreationDate": "2017-09-12T0:49:23.303Z",
      "Shared": false,
      "OwnerID": 0,
      "CategoryID": 8,
      "StageID": 1,
      "Company": "",
      "WebAddress": "",
      "Title": "",
      "FileAs": "Test Test",
      "Source": 0,
      "Notes": "",
      "Custom1": "",
      "Custom2": "",
      "Custom3": "",
      "Custom4": "",
      "Custom5": "",
      "Custom6": "",
      "Custom7": "",
      "Custom8": "",
      "Custom9": "",
      "Custom10": "",
      "Custom11": "",
      "Custom12": "",
      "Custom13": "",
      "Custom14": "",
      "Custom15": "",
      "Custom16": "",
      "Custom17": "",
      "Custom18": "",
      "Custom19": "",
      "Custom20": "",
      "BusinessID": 0,
      "RegionID": 0,
      "LastChangedDate": "2017-09-12T20:49:23.303Z",
      "ListID": null
    }

I work at PoliteMail and have made an internal request to get the documentation updated to fix the capitalization of IsAnonymous.


UPDATE: The documentation has been updated with the correct capitalization. http://kb.politemail.com/?p=1349

like image 199
I. Meredith Avatar answered May 07 '26 05:05

I. Meredith


From the error log, server side may not handle the line break \r\n.

Can you try to remove the line break of your payload?

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"}
like image 45
DLavid Avatar answered May 07 '26 04:05

DLavid