Home > Net >  Python read POST request content and write it to txt file
Python read POST request content and write it to txt file

Time:01-17

I am quite new to python and I feel that what I want to do is not really complicated but I don't find the answer. I want to communicate between two scripts (running 24/24), I thought sending POST requests would be a great idea.

So I receive POST requests using flask with some content (characters as you can see in the example below). 172.31.128.1 - - [16/Jan/2023 21:58:47] "POST /?c51d94c2efccaa2092ad1028285549 HTTP/1.1" 405 -

I want to write the content in a txt file when I receive those requests. I know how to write in such files but I do not know how to get from receiving POST requests to saving content to variables (and so write them to files). Thank you very much !

CodePudding user response:

It would be better to use a message broker like Rabbit MQ or ... to make two program's talk to each other, anyway there's another question with the same problem (reading request data in flask) here, hope this solves you're problem.

CodePudding user response:

when you get the post request in a specific route of your flask server you can using request get the data in the POST, like:

from flask import request
#...
@app.route('/post-something', methods=['POST'])
def hello():
    #the json in the post request
    some_json=request.get_json()

    #the data of the request
    data=request.data

    #save the data in a file
    #...

    return "Done!"

  • Related