尝试使用Python的requests.post()查询NFIB SBET的REST API时出错。

huangapple go评论61阅读模式
英文:

Error trying to query NFIB SBET's REST API using Python's requests.post()

问题

以下是您提供的代码部分的中文翻译:

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"}
        ]
}
resp = requests.post(url, data=data)
print(resp.json())

希望这可以帮助您。如果您有任何其他问题,请随时提出。

英文:

Essentially trying to get data as per the procedure outlined here: http://www.nfib-sbet.org/developers/

My code is as follows:

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"}
                          ]
                }
resp = requests.post(url,data=data)
print(resp.json())

The error I am getting:

{'error': [{'context': None, 'message': 'Failed to call database stored procedure.\nCDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1318 Incorrect number of arguments for PROCEDURE cube_survey.getIndicators; expected 5, got 1', 'code': 500}]}

I have tried to play with the format of my data dictionary and passing it to headers,json,params within requests.post(), but I always get error 400...

Any help appreciated!

答案1

得分: 0

解决方法是在URL中也要追加?app_name=sbet,像这样:

https://open.api.nfib-sbet.org/rest/sbetdb/_proc/getIndicators?app_name=sbet

完整代码如下:

import requests
import json
from datetime import datetime

url = "https://open.api.nfib-sbet.org/rest/sbetdb/_proc/getIndicators?app_name=sbet"

payload = json.dumps({
    "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.today().year
        },
        {
            "name": "maxMonth",
            "param_type": "IN",
            "value": 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"
        }
    ]
})

headers = {
    'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

然而,我有个不好的消息告诉你,这个API从2017年起就不再受支持,最后的数值来自2017年6月:

[
    {
        "monthyear": "2017/6/1",
        "OPT_INDEX": "103.653",
        "expand_employ": "15",
        "plan_capital": "30",
        "plan_invent": "4",
        "expected_bus_cond": "33",
        "expected_real_sales": "18",
        "invent": "-3",
        "job_openings": "30",
        "expected_cred_cond": "-3",
        "good_time_expand": "21",
        "past_earn": "-10"
    }
]
英文:

Solution is to ALSO append ?app_name=sbet in the url, like this:

https://open.api.nfib-sbet.org/rest/sbetdb/_proc/getIndicators?app_name=sbet

Full code looks like this:

import requests
import json
from datetime import datetime

url = "https://open.api.nfib-sbet.org/rest/sbetdb/_proc/getIndicators?app_name=sbet"

payload = json.dumps({
  "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.today().year
    },
    {
      "name": "maxMonth",
      "param_type": "IN",
      "value": 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"
    }
  ]
})
headers = {
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

However I got bad news for you, the api is not supported since 2017, the last values are from 2017 June:

[
    {
        "monthyear": "2017/6/1",
        "OPT_INDEX": "103.653",
        "expand_employ": "15",
        "plan_capital": "30",
        "plan_invent": "4",
        "expected_bus_cond": "33",
        "expected_real_sales": "18",
        "invent": "-3",
        "job_openings": "30",
        "expected_cred_cond": "-3",
        "good_time_expand": "21",
        "past_earn": "-10"
    }
]

huangapple
  • 本文由 发表于 2023年2月19日 06:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定