【问题标题】:JSONDecodeError: Expecting value: line 1 column 1. Python requestsJSONDecodeError:期望值:第 1 行第 1 列。Python 请求
【发布时间】:2021-07-17 07:52:53
【问题描述】:

我将获取我的数据,但我收到了标题所示的错误。我正在请求一个没有数据但在 url 中有参数的帖子

这是我的代码

def post_and_get_id(self):
    url = 'https://heremyurl_post'
    data = {
        'last_name': self.last_name,
        'first_name': self.first_name,
        'is_admin': self.is_admin,
        'is_active': True
    }
    headers = {'Authorization': 'Bearer %s' % self.authentication(), 'Content-type': 'application/json'}
    payload = json.dumps(data)
    request = requests.post(url, data=payload, headers=headers)
    response = request.json()
    return response['id']

def get_client(self):
    id = self.post_and_get_id()
    url = f'https://my_url_get_data?id={id}'
    headers = {'Authorization': 'Bearer %s' % self.authentication(), 'Content-Type': 'application/json'}
    request = requests.post(url, headers=headers)
    print(request.content)
    return request.json()

追溯

Traceback (most recent call last):
  File "main.py", line 70, in <module>
    print(object.get_client())
  File "main.py", line 66, in get_client
    return request.json()
  File "/venv/lib/python3.8/site-packages/requests/models.py", line 910, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这是我的步骤,首先我将数据发布到 URL 以获取 id,使用该 id,我可以通过发布请求获取数据。不幸的是,它不起作用self.authentication() 是一种令牌方法。除了 get_client() 方法外,一切正常。请问有人可以帮我吗?任何帮助将不胜感激!

附言

使用我在邮递员中发布的那个 ID,它可以工作,但在我的代码中却不行(

【问题讨论】:

  • 响应码本身是什么?也许403?检查添加用户代理!
  • 我在 pycharm 中进行了调试,它显示了受保护变量中的所有数据,但我如何将其可视化?
  • print(request) !
  • 它只返回状态码 202:
  • 所以你没有有效的回应! print(request.text)!

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


【解决方案1】:

好吧,在第一个函数中,您必须返回您使用的 Auth 令牌。

您不需要从第二个函数中调用它,因为它会生成一个新函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 2016-04-07
    • 2019-07-04
    • 2020-04-18
    • 2013-05-10
    • 2019-02-25
    • 2020-02-03
    相关资源
    最近更新 更多