【问题标题】:Get Json rest API parsing in Python在 Python 中获取 Json REST API 解析
【发布时间】:2023-03-22 07:57:01
【问题描述】:

在通过 json 响应中的 API 获取数据方面需要一些帮助。 我正在获取另一个 api 的数据,但这个看起来有点不同所以问。

** 这是提供者提供的:-**

e.g. POST https://yyyy/analytics/generi
{
    "report_definition_id": "yyyyyyyyyy",
    "parameters": {
        "from": "2021-01-26",
        "to": "2021-02-05"  Can you please edit this to include yesterday date always?
    }
}
Usual header:
Authorization:Bearer zzzzzzzzzzzzzzzzzzzzzzz
Content-Type:application/json

我正在将它用于另一个 API,所以你能修改它吗

def fp_data_processing():
    authurl="https://xxxxxxx/connect/token"
    payload = "grant_type=client_credentials&scope=read admin"
    headers={
             'Authorization': "Basic xxxxxxxx",
             'Content-Type': "application/x-www-form-urlencoded"
            }
    response = requests.request("POST", authurl, data=payload, headers=headers)
    response_json  = response.json()

    #Only one when successful API response :200
    if(response.status_code==200):
        logging.info('API Authorization Completed:Moving to Extract:')
        purchaseurl = "https://yyyyy/analytics/generic"
        start=time.time()
        querystring=  {"report_definition_id": "zzzzzzzzzzzzzzz","parameters": {"from": "2021-01-26","to": "2021-02-05"}}
        headers =     {'Authorization':"Bearer "+response_json['access_token']}
        response = requests.request("GET", purchaseurl, headers=headers, params=querystring)

但出现此错误

Traceback (most recent call last):
  File "tcustomer_kpi.py", line 60, in <module>
    fp_data_processing()
  File "tcustomer_kpi.py", line 36, in fp_data_processing
    if response.json():
  File "/xxxxxxxxxxxxxxx/env/lib/python3.5/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【问题讨论】:

  • 在解码前通过查看“JSONDecodeError("Expecting value", s, err.value) from None" 来检查 var 是否为 None
  • @juan 怎么查?
  • @juan 你能告诉我我使用的代码和结构是否正确吗?

标签: python json api


【解决方案1】:

检查你得到的响应是否是json

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

try:
   response_json = response.json()
except ValueError:
   # Do something if you dont get a json

【讨论】:

  • post 响应的响应是 json,但之后以下响应显示解码错误 if(response.status_code==200): logging.info('API Authorization Completed:Moving to Extract:' ) purchaseurl = "yyyyy/analytics/generic" start=time.time() querystring= {"report_definition_id": "zzzzzzzzzzzzzzz","parameters": {"from": "2021-01-26","to": "2021- 02-05"}} headers = {'Authorization':"Bearer "+response_json['access_token']} response = requests.request("GET", purchaseurl, headers=headers, params=querystring)
  • @json 我也在使用 headers={ 'Authorization': "Basic xxxxxxxx", 'Content-Type': "application/x-www-form-urlencoded" } 但提供说要使用内容类型 :application/json 但是当我使用它时它显示无效请求
猜你喜欢
  • 2016-05-09
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 2019-04-14
  • 1970-01-01
  • 2017-03-28
相关资源
最近更新 更多