Invalid Post: 400 - Bad Request with Python requests -
i'm using politemail's odata restful api (doc: politemail api entity directory)
i'm trying create contact inside of politemail requests.post()
.
below steps
load libraries
import requests import json # point 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 within entity, can't post.
post request
below structure given in documentation
payload = { "id":"0", "name":"test list", "description":"does 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: request contains entity body no content-type header. inferred media type 'application/octet-stream' not supported resource."
but when add header content type:
r = requests.post(url, data=json.dumps(payload),auth=auth, headers = {"content-type": "application/json"})
i'm told:
400 "the request invalid." error has occurred.\r\n","type":"","stacktrace":""
i've tried following, no avail:
json=
instead ofdata=
- removing quotes num
- changing id value other 0
- removing newline characters payload
any appreciated!
update
the errors coming parsed text of response below full response readout when use beautifulsoup
{ "odata.error":{ "code":"","message":{ "lang":"en-us","value":"the request invalid." },"innererror":{ "message":"contact : error has occurred.\r\n","type":"","stacktrace":"" } } }
i able create list sample fixing capitalization of false false , isanonymous isanonymous in payload.
{ "id":"0", "name":"test list", "description":"does work", "isnewsletter":"0", "shared":false, "creationdate":"2014-11-19t23:00:00.000z", "activestate":"1", "isanonymous":false, "businessid":"0", "regionid":"0" }
you mentioned creating contact @ beginning of post. able create contact following.
{ "displayname": "test test", "firstname": "test", "lastname": "test", "email": "test@politemail.com", "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 @ politemail , have made internal request documentation updated fix capitalization of isanonymous.
update: documentation has been updated correct capitalization. http://kb.politemail.com/?p=1349
Comments
Post a Comment