requests.exceptions.httperror:400客户端错误:URL的不良请求



我正在尝试使用Python请求在数据库中创建一个对象。我可以使用其他URL来执行此操作,但是对于这个特定的URL,我会遇到错误。我不确定实际请求或URL是否存在问题。创建需要四个项目,因此我暂时只关注这些项目。

下面您将根据文档找到请求有效载荷的示例:

def create_opportunity(self, data):
    try:
        r = requests.post(
            self.URL + 'sales/opportunities', data=data,
            headers=self.Header)
        r.raise_for_status()
    except:
        raise
    return r.json()
create_opp = '{"name": "My Opportunity", "primarySalesRep": {"name": "John Doe"}, "company": {"name": "My Company"}, "contact": {"name": "Jane Doe"}}'
opportunity = objCW.create_opportunity(create_opp)

有效载荷示例

{
  "name": "string",
  "primarySalesRep": {},
  "company": {},
  "contact": {}
}

primarsysalesrep

"primarySalesRep": {
    "id": 0,
    "identifier": "string",
    "name": "string",
    "_info": { }
},

公司

"company": {
    "id": 0,
    "identifier": "string",
    "name": "string",
    "_info": { }
},

联系人

"contact": {
    "id": 0,
    "name": "string",
    "_info": { }
},

在您的代码create_opp中是字符串。您不应该在post()的CC_3功能的data=关键字中传递字符串。

鉴于服务器返回JSON(return r.json()(,我可以猜它也会收到JSON。尝试做这样的事情:

def create_opportunity(self, data):
    r = requests.post(self.URL + 'sales/opportunities', json=data, headers=self.Header)
    r.raise_for_status()
    return r.json()
create_opp = {
    "name": "My Opportunity", 
    "primarySalesRep": {"name": "John Doe"},  # maybe "id" or "identifier" is required? 
    "company": {"name": "My Company"},  # maybe "id" or "identifier" is required? 
    "contact": {"name": "Jane Doe"},
}
opportunity = objCW.create_opportunity(create_opp)

最新更新