简体   繁体   中英

How to receive HTTP POST requests in Python?

Task:

User types into a form a char eg "hello".

this should be send as an post requests to python, because it is later analyzed via a python script

here is my server side code:

from flask import Flask, request
app = Flask(__name__)

@app.route('/', methods=['POST'])


def result():
    print(request.form['foo']) # should display 'bar'
    return 'Received !' # response to your request.

if __name__ == '__main__':
    app.run()

My html code:

<input class="input-text-input" type="text" placeholder="values" name="website">

How can i get the user input from the php form? What would be the best way? The examples in the internet etc. are overloaded. Maybe i have a general mistake.

To make a request from html form to your python flask API, you can do it this way,

HTML FORM

<form action="{{ url_for('addWebsiteUrl') }}" method="post">
    <input class="input-text-input" type="text" placeholder="values" name="website">
    <input type="submit" value="Submit">
</form>

FLASK API:

from flask import Flask, request
app = Flask(__name__)

@app.route('/', methods=['POST'])
def result():
    print(request.form['website']) # should display 'website'
    return 'Received !' # response to your request.

if __name__ == '__main__':
    app.run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM