Mark
Mark

Reputation: 1551

Requests throws 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)

back-end:

@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

Error output:

...
...
 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 ?

Upvotes: 1

Views: 1864

Answers (1)

Dušan Maďar
Dušan Maďar

Reputation: 9909

Change

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

to

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 (char 0)

usually means something else, not JSON, was returned.


Maybe the problem is

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

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

Upvotes: 1

Related Questions