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

NetSuite REST API成功返回204无内容,如何获取创建的记录ID?

NetSuite REST API创建销售订单返回204及获取ID的解决方案

为什么默认返回204 No Content?

NetSuite REST API在创建资源(如销售订单)时,默认遵循RESTful设计的精简原则——返回204状态码表示请求成功,但无需返回响应体,以此减少网络传输的数据量、提升接口性能,这是API的既定默认行为,并非调用错误。

如何获取已创建的销售订单ID?

有两种可靠方式可以获取创建后的记录ID:

方法1:添加Prefer: return=representation请求头

在请求头中加入该字段后,API会返回201 Created状态码,同时响应体中会包含完整的销售订单记录,其中就包含所需的ID。

修改你的代码如下:

import json

def create_sales_order():
    url = f"https://{url_account}.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder"

    data = {
        "entity": {
            "id": "000"
        },
        "item": {
            "items": [
                {
                    "item": {
                        "id": 25
                    },
                    "quantity": 3,
                    "amount": 120
                }
            ]
        },
        "memo": "give me money",
        "department": "109"  # 注意:NetSuite字段名通常为小写开头,修正为department避免报错
    }

    # 给请求头添加Prefer字段
    headers["Prefer"] = "return=representation"
    headers["Content-Type"] = "application/json"  # 确保Content-Type正确设置
    
    body = json.dumps(data)

    response = client.post(url=url, headers=headers, data=body)
    
    if response.status_code == 201:
        order_data = response.json()
        sales_order_id = order_data["id"]
        print(f"创建的销售订单ID:{sales_order_id}")
        print("完整订单数据:", order_data)
    else:
        print(f"请求失败,状态码:{response.status_code}")
        print(response.text)

方法2:从响应的Location头中提取ID

即使不修改请求头,API成功创建记录后,响应头中会包含Location字段,其值为新创建记录的API访问地址,地址末尾的数字即为订单ID(例如Location值为https://xxxx.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder/12345,则12345是订单ID)。

提取ID的示例代码:

response = client.post(url=url, headers=headers, data=body)

if response.status_code == 204:
    location_header = response.headers.get("Location")
    if location_header:
        sales_order_id = location_header.split("/")[-1]
        print(f"创建的销售订单ID:{sales_order_id}")
    else:
        print("未获取到Location响应头")

注意事项

  • 务必确保请求头中的Content-Type设置为application/json,否则API可能无法正确解析请求体。
  • 验证你的账号权限是否允许访问创建后的销售订单数据,避免因权限不足导致无法获取返回内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 05:40:37