Python Forum
expecting value: line 1 column 1 (char 0) in print (r.json)) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: expecting value: line 1 column 1 (char 0) in print (r.json)) (/thread-27359.html)



expecting value: line 1 column 1 (char 0) in print (r.json)) - loutsi - Jun-04-2020

hi i experience a problem. So i deploy a model in flask


from flask import Flask, request, jsonify#--jsonify will return the data
from keras.models import load_model
import numpy as np
import pandas as pd

#creates the web service running on port 127.0.0.1:12345 and answer post requests on address of the machine/api
app = Flask(__name__)

model=load_model('modelfordeploy.h5')

@app.route("/")
def hello():
    return "machine learning model APIs!"


@app.route('/api', methods=['GET','POST'])
def predict():
    
    # get the json send from the post as data
    data=request.get_json(force=True)
    print (data)
    
    # i want 6 variables to make the prediction
    #i expected jason to have the following values
    predict_request=[data["accx"], data["accy"], data["accz"], data["gyrx"], data["gyry"], data["gyrz"]]
    print (predict_request)
    
    predict_request= np.array(predict_request)
    print (predict_request)
    type (predict_request)
    
    predict_request = np.asarray( predict_request).reshape(-1, 1, 6)

    pred= model.predict(predict_request)
    print (pred)
    
    output=[pred[0]]
    print (output)
    
    #take a list of dictionaries and convert them to json
    return jsonify(results=output)
   

if __name__ == '__main__':
    app.run(port=8000, debug=True) #threaded=False)
and then make a post request

import requests, json
import pandas as pd
import numpy as np

url="http://127.0.0.1:8000/api"

data= json.dumps ({"accx":0.660583, "accy":0.454468, "accz":-0.585022, "gyrx":32.366615, "gyry":27.206556, "gyrz":-23.471800})
r=requests.get(url, data)
print (r.json())
as I result I take back the following error. I can figure out how to fix it
Error:
File "request.py", line 22, in <module> print (r.json()) File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\site-packages\requests\models.py", line 898, in json return complexjson.loads(self.text, **kwargs) File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\json\__init__.py", line 348, in loads return _default_decoder.decode(s) File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\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) (tensorflow_env) C:\Users\aggelos\Desktop\new_try>



RE: expecting value: line 1 column 1 (char 0) in print (r.json)) - nuffink - Jun-05-2020

hi
I am slightly confused here as you mention that the second piece of code is posting a request and yet you are using the requests GET function. If you are looking to post that data variable object in the second piece of code then the parameters for that function are requests.post(url, data, json), I have not looked at the requests package specifically to see what they appertain to.
At the moment you are trying to decode the data expected from the GET which I will be not there i.e. None and hence the char(0),the variable r in the post will actually contain the response code I expect something like r: <Response [500]> for example if the whole thing failed and consequently you can not json that either so just chane it for a print® if you want to test the response result


RE: expecting value: line 1 column 1 (char 0) in print (r.json)) - buran - Jun-05-2020

instead of r.json(), try to print r.text so you can see what you really get


RE: expecting value: line 1 column 1 (char 0) in print (r.json)) - nuffink - Jun-05-2020

Hi.

I replicated this in my own environment and as said your GET when you invoke the request is received by your api code which then tries to retrieve the body of the request (line 20 first code set)but the second parameter for requests.get package is request parameters aka ?x=12345 style query parameters,if as i suspect you wish to post the dictionary data in the second code set then the request needs to be a post method, this then is retrieved by the code on code set 1 line 20, and although failed for me the rest of the logic, it does return to the "r" variable the response code from flask, in my case HTTP 500, but worked to a fashion.