【问题标题】:Python requests:- simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)Python 请求:- simplejson.errors.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)
【发布时间】:2021-10-20 11:21:24
【问题描述】:

我正在尝试使用 python requests 库获取一些订单详细信息。该 API 在 postman 上运行良好,但是当我尝试使用 python 运行它时,会出现以下错误。

simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我正在使用以下python代码调用截图中的API:

import requests
url = "https://xxxxxxxxxxxxxx/purchases"
header = {"Content-Type":"application/json", "Authorization":'token {}'.format('eyXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')}
data = {"from_date":"2021-06-15T07:19:32", "to_date":"2021-09-16 00:01:09"}
response = requests.get(url, params=data, headers=header)
print(response.json())

我得到的结果是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【问题讨论】:

  • 你有没有看过print(response.text)的输出并且看起来它也是正确的json格式
  • 是的,我已经尝试打印响应文本,它就像一个 HTML

标签: json python-3.x api python-requests


【解决方案1】:

问题是我需要在标头部分的 Autorization 参数中传递的令牌前面添加一个文本 "Bearer ",如下所示,因为该令牌将作为承载令牌传递:

header = {"Content-Type":"application/json", "Authorization":'Bearer eyXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2021-12-05
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多