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

使用Python requests.post()调用NFIB SBET REST API报错求助

解决NFIB SBET API调用参数错误问题

核心问题分析

错误提示Incorrect number of arguments for PROCEDURE cube_survey.getIndicators; expected 5, got 1说明API后端未正确解析你传递的5个参数,根源是请求格式错误:你用data=data发送了表单编码的数据,而API期望接收JSON格式的请求体。

解决方案

将requests.post的data参数替换为json参数,requests会自动把字典序列化为JSON字符串,并设置正确的Content-Type: application/json请求头,确保API能正确识别每个参数。

修改后的完整代码

import requests
import datetime

url = "http://open.api.nfib-sbet.org/rest/sbetdb/_proc/getIndicators"
data = {
    "app_name": "sbet",
    "params": [
        {"name": "minYear", "param_type": "IN", "value": 1974},
        {"name": "minMonth", "param_type": "IN", "value": 1},
        {"name": "maxYear", "param_type": "IN", "value": datetime.datetime.today().year},
        {"name": "maxMonth", "param_type": "IN", "value": datetime.datetime.today().month},
        {"name": "indicator", "param_type": "IN", "value": "OPT_INDEX,expand_employ,plan_capital,plan_invent,expected_bus_cond,expected_real_sales,invent,job_openings,expected_cred_cond,good_time_expand,past_earn"}
    ]
}

# 使用json参数发送JSON格式请求
resp = requests.post(url, json=data)
print(resp.json())

补充说明

  • 确保代码顶部导入了datetime模块,否则datetime.datetime.today()会触发未定义错误。
  • 若你之前尝试手动设置headers={"Content-Type": "application/json"}并配合data=json.dumps(data),效果和使用json=data一致,但后者更简洁且不易出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 09:31:01