邮件无效:400 - Python请求错误请求

时间:2017-09-12 02:22:39

标签: python python-requests odata

我正在使用PoliteMail的ODATA RESTful API(文档:PoliteMail API Entity Directory

我正在尝试使用requests.post()在PoliteMail中创建一个联系人。

以下是我的步骤

加载库

import requests
import json

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

认证信用

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

获取请求

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

我可以在这个实体中获取任何内容,但不能发布。

POST请求

以下是结构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"
}

我的要求:

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

这是输出

  

415:请求包含实体主体但没有Content-Type标头。此资源不支持推断的媒体类型“application / octet-stream”。“

但是当我为内容类型添加标题时:

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

我被告知:

  

400“请求无效。”发生错误。\ r \ n“,”键入“:”“,”stacktrace“:”“

我尝试过以下内容,但无济于事:

  • json=代替data=
  • 从num
  • 中删除引号
  • 将ID值更改为0以外的其他值
  • 从有效负载中删除换行符

非常感谢任何帮助!

更新

当我使用BeautifulSoup

时,错误来自以下响应的解析文本是完整的响应读数
{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"The request is invalid."
    },"innererror":{
      "message":"contact : An error has occurred.\r\n","type":"","stacktrace":""
    }
  }
}

2 个答案:

答案 0 :(得分:2)

我能够通过将 False 的大小写固定为 false 来创建包含示例的列表,有效负载中 isAnonymous IsAnonymous

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

您还提到了在帖子开头创建联系人。我能够使用以下内容成功创建联系人。

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

我在PoliteMail工作并发出内部请求以更新文档以修复 IsAnonymous 的大小写。

更新:文档已使用正确的大写更新。 http://kb.politemail.com/?p=1349

答案 1 :(得分:1)

从错误日志中,服务器端可能无法处理换行符\r\n

您可以尝试删除有效负载的换行符吗?

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