【问题标题】:Requests throws json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)请求抛出 json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)
【发布时间】:2021-10-21 18:04:54
【问题描述】:

我正在从我的一个端点检索数据:

    for index in self.indices_to_fetch:
        response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()
        print(response)

后端:

@users_blueprint.route('/fetch_one_image', methods=['POST'])
def fetch_one_image():
    post_data = request.get_json()
    index = post_data.get('index')
    image_name = Photo.query.filter(Photo.owner.has(User.id==id)).first()
    response_obj = {
        'image_name': image_name
    }
    return jsonify(response_obj), 200

错误输出:

...
...
 File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
   File "/home/mark/front_end_android/venv/lib/python3.7/site-packages/kivy/uix/screenmanager.py", line 419, in _on_complete
     self.screen_in.dispatch('on_enter')
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "main.py", line 338, in on_enter
     self.display_results()
   File "main.py", line 383, in display_results
     self.fetch_one_image()
   File "main.py", line 342, in fetch_one_image
     response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()
   File "/home/mark/front_end_android/venv/lib/python3.7/site-packages/requests/models.py", line 898, in json
     return complexjson.loads(self.text, **kwargs)
   File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
     return _default_decoder.decode(s)
   File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
   File "/usr/lib/python3.7/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)

我在其他端点上使用了类似的方法,但没有出现错误。只有这一个在烦我。我能做什么?

【问题讨论】:

  • 我从import json; json.loads('') 得到同样的错误,这表明被调用的 API 返回一个空字符串。
  • @buran 在响应中包含状态代码不是问题。其他端点使用它没有问题。

标签: python flask python-requests


【解决方案1】:

改变

response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()

response = requests.post('http://localhost/fetch_one_image', json={'index': index})

看看实际返回了什么。

json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)

通常意味着返回了其他内容,而不是 JSON。


也许问题是

image_name = Photo.query.filter(Photo.owner.has(User.id==id)).first()

因为它实际上获取了一个 Photo 对象(或者甚至可能是 None,如果过滤器 [与 .fist() 结合使用] 返回任何内容),而不仅仅是它的名称。尝试类似[这只是一个猜测,因为我不知道你的模型结构]

image = Photo.query.filter(Photo.owner.has(User.id==id)).first()
response_obj = {'image_name': image.name}
return jsonify(response_obj), 200

【讨论】:

  • 是的。响应对象给出状态码 500。数据库查询有问题。
  • 我认为您正在获取一个 Photo 对象,而不仅仅是它的名称;答案已更新。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2020-11-11
  • 2017-05-09
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多