1

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

user1584421
  • 3,499
  • 11
  • 46
  • 86

1 Answers1

1

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
Matteo Pasini
  • 1,787
  • 3
  • 13
  • 26
  • Thank you. This worked, however, when i do `print(sent_files)`, i get `ImmutableMultiDict([('file', )]) `. Do i have to do any post processing to the `sent_files`, so as to be recognied as json or maybe a dictionary? – user1584421 May 31 '21 at 14:44
  • 1
    Yes, `request.files` is a dictionary that contains all the files that the client sent to the server. To get it you need to do: `json_file = sent_files['file']`obviously if you send it with different name use the appropriate name and not 'file'. To read the file content, see this stack overflow question: https://stackoverflow.com/questions/20015550/read-file-data-without-saving-it-in-flask – Matteo Pasini May 31 '21 at 14:50
  • I tried this `json_file = sent_files[test.json]` but i got ` json_file = sent_files[test.json] NameError: name 'test' is not defined`. Also, this approach means i will have to hardcode the file name. – user1584421 May 31 '21 at 15:06
  • 1
    In your case you can access it by `json_file = sent_files['file']`, the params of the dictionary is the file name, the file name is not the name that the file has on your pc, but the name that you give to the file when you send it with the http request: `curl -X POST -F file=@/home/user1/Desktop/test.json 0.0.0.0:5000/testjson`, in your case **file** =path. test.json give you that error because the params need to be a string. I hope I was clear, otherwise ask for other clarifications. – Matteo Pasini May 31 '21 at 19:45
  • Thank you very much! Printing `json_file` gave me this: ` 127.0.0.1 - - [31/May/2021 23:29:55] "POST /process HTTP/1.1" 200 -`. I have two more relevnat questions - but in order for you to get more points, i will make a separate question than ask here (of course its up to you if you answer or not). I will link it here in a comment. – user1584421 May 31 '21 at 20:32
  • certainly, no problem – Matteo Pasini May 31 '21 at 20:37
  • Thank you very much! Here it is, if you want to give it a try. https://stackoverflow.com/questions/67779996/convert-received-json-to-dictionary-and-save-to-local-disk – user1584421 May 31 '21 at 20:40