Don't Stop 'Til You Get
Today, we're going to learn about an alternative way of getting data from forms to the webserver.
So far, we've used post, which (kinda) packages up all the data from the form and sends it to the server.
We can think of this as the form controlling when the data is sent.
With the get method, the request for the data comes from the webserver. It effectively says gimme that data to the form.
You've probably seen get in use before. If you've ever seen a URL with a ? after the website name, then a bunch of = and maybe & symbols, then that website is using get.
So What's The Difference?
I'm glad you asked!
With post, the data in the form can't be seen by your web browser. Once it's sent, it's gone. This means that you can't bookmark or share a URL based on post data because it will be different for each user. Ever tried to drop those SO subtle present hints by sharing a shopping cart link? Only to get a link that doesn't show any shopping cart? Yep, that's the problem with post. The link for you will be different than the one for other users.
Get data encodes the data in the URL so we can bookmark & share it and get the same results from the page.
π Let's add some code to our Flask boilerplate to show how this works.
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=["GET"])
def index():
return 'Hello from Flask'
app.run(host='0.0.0.0', port=81)
Comments
Post a Comment