请求抛出 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) [英] Requests throws json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

查看:38
本文介绍了请求抛出 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm retrieving data from one of my endpoints:

    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)

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

I'm using similar approach on my other endpoints but getting no errors. Only this one is bugging me. What can I do ?

推荐答案

更改

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

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

并查看实际返回的内容.

and take a look at what is actually returned.

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char0)

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

通常表示返回了其他内容,而不是 JSON.

usually means something else, not JSON, was returned.

也许是问题

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

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

as that actually fetches a Photo object (or perhaps even None, if the filter [in combination with .fist()] returns nothing), not only it's name. Try something like [it's just a guess, since I don't know your model structure]

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

这篇关于请求抛出 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆