Flask – receive and print json

Issue

I want my flask server to accept a json file and print it.

I am sending the json with:

curl -X POST -F file=@/home/user1/Desktop/test.json 0.0.0.0:5000/testjson

This is the server’s code:

@app.route('/testjson', methods=['POST'])
def trigger_json_test():
   POST_content = request.get_json()
   print(POST_content)
   return make_response(json.dumps({'fileid': "ok"}), 200)

While the server responds with the correct response, all i see to the terminal regarding the print() is None

Solution

request.get_json() allows you to get the json passed in the body of the http request (obviously if you have specified application/json as the content type of the request in the header).

If you send the json as a file, then you can get it via the files property of request object:

sent_files = request.files #request.files contains all file sent by the client

Answered By – Matteo Pasini

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published