In this topic, we will discuss what happens when you access a site from a browser. It's pretty straightforward: we launch a browser, enter an address in the address bar, and press Enter. Then, magically, the site is on the screen. How does this happen? Let's figure it out!
Accessing from browser
Let's write a common web application using Flask. As usual, we need to import the necessary objects and methods first:
from flask import Flask, make_response
Then, we can create a simple view function:
app = Flask('main')
@app.route('/')
def main_view():
return 'Hello there, in Main Page!'
Finally, we can launch our app!
app.run()
The standard output will indicate that everything is fine:
Okay, the standard app writing ritual is completed. Time to move on! Let's open a browser and type in 127.0.0.1:5000. We will see our app on the screen; a record of the executed request will appear in the window with the running program:
Let's discuss what has happened: we have opened the browser and entered 127.0.01:5000 in the address bar. The browser has sent a GET request to our application. The app has processed this request and returned the response. We see what we see. Unfortunately, we are usually not concerned with the specific processes behind a POST request through the browser. But every time you register, log in, send your contact details or place an order in an online store, you make a POST request that contains your username, password, full name, and phone number, and so on. This data is sent to the server that processes it and notifies the service workers.
POST requests from the browser
Let's look at how we can send a POST request using a browser. To do this, we will write a simple application that returns a form using the POST method as we have done in the previous topics. The code will look as follows:
from flask import Flask, request
MAIN_PAGE_GET = '''
<h1>Welcome!</h1>
<form method='post'>
<input type='submit' value='Push me!'>
</form>
'''
MAIN_PAGE_POST = '''
<h1>Perfect!</h1>
<p>You succesfully sent POST request via your browser!</p>
'''
app = Flask('main')
@app.route('/', methods=['POST', 'GET'])
def main_view():
if request.method == 'POST':
return MAIN_PAGE_POST
elif request.method == 'GET':
return MAIN_PAGE_GET
app.run()
Note that we use a multi-line string framed by the ''' quotes. We may skip the escape of regular quotes inside. This simplifies the coding. We write very specific HTML code and save it to a string variable. We also use the triple-quote strings and to keep line breaks. It allows us to make a small template for a small web page.
Now, when we launch our application and go to the main page, we will see the following picture:
As in the first example, we have accessed the page and sent a GET request. And now we will press the button:
We have done it again! By clicking on the linked button, we could send this POST request directly from the browser.
Conclusion
We have learned how the browser obtains the necessary information from us and discussed the procedure that occurs when you open a website or web application and sends the POST and GET requests from the browser.