0

I've wrote a little flask application. I send a request with json data (python script) to the flask server (ubuntu 18.04 with python 3.6.7), and the server evaluate these data...

Client:

data =  {   'serial': '12345'
            'printerConfig':  {
                    'virtualPrinter1':    'physicalPrinter1',
                    'virtualPrinter2':    'physicalPrinter2',
                    'virtualPrinter3':    'physicalPrinter3'
         }
}
r = requests.get("http://127.0.0.1/webservice", json=data)

Server:

@app.route('/', methods=['GET', 'POST'])
def webservice():
if request.method == 'GET':
    serial = request.json['serial'] if 'serial' in request.json else None
    printerConfig = request.json['printerConfig'] if 'printerConfig' in request.json else None

I have tested this code in pyCharm with the development server and always works fine.

After that I have tried to implement that to an Apache2 server. Now I get the following error message in apache-error.log.

ERROR:flask.app:Exception on / [GET]
Traceback (most recent call last):
    ...
    File "/var/www/html/webservice/webservice.py", line xx, in webservice
        serial = request.json['serial'] if 'serial' in request.json else None
TypeError: argument of type 'NoneType' is not iterable

Print of request.json shows only None. Exactly the same code on the development server is running.

Instead of request.json I also tried request.data, with the same result.

Is there a specially setting for the Apache2? Has anyone an idea?

Best regards flo

flo
  • 1

1 Answers1

0

You are making a get request.

Try making a post request to your webservice

r = requests.post("http://127.0.0.1/webservice", json=data)

You will never include the json="" when making a get... that is for only posting/updating json to the webserver

Remember, GET in http terms is only for requesting content from a URL endpoint. POST is to send new content to the URL endpoint. GET/POST

Another quick point... Try just printing request.json on your back end/flask app before the serial = request.json['serial'] if 'serial' in request.json else None etc... just to make sure that the data is arriving correctly upon being posted.

  • Thank you for your answer. I have changed GET to POST, unfortunately with the same result :-( – flo Mar 20 '19 at 08:03