You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python requests创建数据库对象时遇400错误求助

Fixing the 400 Bad Request When Creating an Opportunity with Python Requests

Hey there, let's break down why you're hitting that 400 error and get your opportunity creation working smoothly. Here are the key issues and actionable fixes:

1. You're Sending JSON in the Wrong Format & Content-Type

Right now, you're passing a JSON string to the data parameter of requests.post(). By default, requests treats this as application/x-www-form-urlencoded data, but your API expects application/json format. This mismatch is a common cause of 400 Bad Request errors.

Fix: Use the json Parameter for JSON Payloads

Requests has a dedicated json parameter that automatically serializes Python dictionaries to JSON and sets the correct Content-Type: application/json header. Update your code like this:

First, pass a Python dictionary instead of a JSON string in your call:

create_opp = {
    "name": "My Opportunity",
    "primarySalesRep": {"name": "John Doe"},
    "company": {"name": "My Company"},
    "contact": {"name": "Jane Doe"}
}
opportunity = objCW.create_opportunity(create_opp)

Then modify your create_opportunity method to use the json parameter:

def create_opportunity(self, data):
    try:
        r = requests.post(
            self.URL + 'sales/opportunities',
            json=data,  # Replace data=data with json=data
            headers=self.Header
        )
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        # Critical: Print the API's error response to get specific details
        print(f"API Error Details: {r.text}")
        raise e
    return r.json()

The r.text printout is game-changing—most APIs will explicitly tell you which field is invalid or missing in the 400 response body.

2. Watch for Typos in Field Names

I spotted a typo in your field structure description: you wrote primarsySalesRep instead of primarySalesRep. Double-check that your request payload uses the correct spelling (your current call code looks right, but it's easy to slip up!).

While the documentation shows empty objects for primarySalesRep, company, and contact, many CRM-style APIs require referencing existing records by id or identifier instead of just name (since names aren't always unique). For example, if the API expects an ID, your payload would need to look like this:

create_opp = {
    "name": "My Opportunity",
    "primarySalesRep": {"id": 123},  # Use the actual rep ID from your system
    "company": {"id": 456},          # Use the actual company ID
    "contact": {"id": 789}           # Use the actual contact ID
}

Use the error details from r.text to confirm this—often the API will say something like "primarySalesRep.id is a required field".

Final Debugging Steps

  1. Run the updated code and check the printed API Error Details—this will give you the exact reason for the 400.
  2. Cross-reference the error message with the API docs to ensure all required fields are included in the correct format.
  3. If you're still stuck, test the same payload with a tool like Postman to rule out Python-specific issues.

内容的提问来源于stack exchange,提问作者garmars

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:54:22