无效的Post:400个错误请求与Python请求

2024-06-16 12:14:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是礼貌邮件的odatarestfulapi(Doc:PoliteMail API Entity Directory

我正在尝试用requests.post()在礼貌邮件中创建联系人。在

下面是我的步骤

加载库

import requests
import json

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

认证证书

^{pr2}$

GET请求

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: The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource."

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

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

有人告诉我:

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

我试过以下几种方法,但没有效果:

  • json=而不是{}
  • 从num中删除引号
  • 将值更改为ID以外的值
  • 从有效负载中删除换行符

非常感谢任何帮助!在

更新

错误来自解析后的文本,下面是我使用BeautifulSoup时的完整响应读数

{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"The request is invalid."
    },"innererror":{
      "message":"contact : An error has occurred.\r\n","type":"","stacktrace":""
    }
  }
}

Tags: theimportauthjsonurlisosrequest
2条回答

通过将有效载荷中False的大写字母改为FalseisAnonymousisAnonymous来创建一个列表。在

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

你在帖子的开头还提到了创建联系人。我成功地与以下人员建立了联系。在

^{pr2}$

我在PoliteMail工作,曾在内部请求更新文档,以修复的大写字母。在


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

根据错误日志,服务器端可能不处理换行符\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"}

相关问题 更多 >