Flask Request Object I - method

Client send the data to server as a global request object(must import request from FLASK)

some important attributes are below:
method − current request method
Form − dictionary object form parameters and their values
Cookies − dictionary object holding Cookie names and values
files − data pertaining to uploaded file
args − parsed contents of query string which is part of URL after a ? charter

method:

GET: get the information stored on that page and send it
HEAD: get only interested in the headers, not the content of the page
POST: post information to that URL and that the server must ensure the data is stored and only stored once
PUT: server might trigger the store procedure multiple times by overwriting the old values more than once
PATCH: used to update partial resources(e.g. update one field)
DELETE: remove the information from the given location
OPTIONS: provides a quick way for a client to figure out which methods are supported by this URL

In HTML4 and XHTML1, the only methods a form can submit to the server are GET and POST,
but with JavaScript and future HTML standards you can use the other methods as well.

example:

login.html

<html>
  <body>
    <form action = "http://<ip/url>:5000/login" method = "post">

      <p>Enter Name:</p>
      <p><input type = "text" name = "nm" /></p>
      <p><input type = "submit" value = "submit" /></p>

    </form>
  </body>

</html>
login.py
from flask import Flask, redirect, url_for, request
app = Flask(__name__)

@app.route('/success/<
name_input>')
def success(
name_input):
    return 'Welcome %s.' %
name_input

@app.route('/login', methods = ['POST', 'GET'])
def login():
    if request.method == 'POST':
       user = request.form['nm']
       return redirect(url_for('success', name_input=user))
    else:
       user = request.args.get('nm')
       return redirect(url_for('success',
name_input=user))

if __name__ == '__main__':
    app.run(host='0.0.0.0')
python login.py

#use your browser open the login.html(must use general http since it's a text file with no route)
#or maybe save it at client and open it at local



Leave comments

authimage

Copyright(c) 2017 - PythonBlogs.com
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved