使用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
相关产品推荐
相关产品推荐

