Do you know how to use methods get and post in an income and expense app? Request methods label how a server is accessed and called. Methods get and post In an income and expense app they represent the use of these two methods, as they tell how the server of an application is being accessed, whose objective is to record all the movements of an account, that is, income and expenses. This way, Here we show you how to use the methods get and post in an income and expense app.
**** request methods
As we mentioned in the article about **** request methods, these are forms of representation in which a code program accesses a server while, at the same time, a client makes a request.
The way to obtain information about **** request methods is varied, as you can observe it from the IDE or programming console where you are writing your code or, on the other hand, from the Google Chrome Devtools extension.
There are several **** request methods that work to obtain a specific response. Below, we present 6 of them:
GET: method to obtain solutions to our requests. HEAD: method that will give you the header responses and not the specific responses. POST: Method that sends data to a resource for modification. PUT: method that is responsible for modifying data. DELETE: method that deletes some data. PATCH: method that partially modifies the data.
How to use get and post methods in an income and expenses app
To manage the form or method of submitting **** requests to a web server, you must pass one of the previously established methods as a parameter. You must take into account the nature of the program or the dependencies of your code programsince not all of them accept the same type of method.
For example, in the event that Flask is not passing the request with type POST, we will have to transform it, as we will see below:
At first, we must be clear where and what methods are receiving the program’s requests to the server, which can be represented with a name after the sign /. In the case of the income and expenses app program, this method is represented by @app.route (‘/new’), which must be instantiated in the views file. The methods that can be passed are set out in the following image:
Parameters to app.route
The next step is to pass the parameters or methods as arguments to this function as follows:
@app.route(‘new’, methods = [‘GET’, ‘POST’])
After passing the parameters, we can start capturing the POST method inside our program, and below the method established above. Do it in the following way:
#in the views.py file
@app.route(‘new’, methods = [‘GET’, ‘POST’])
def new():
if request.method == ‘GET’:
return render_template(«new.html»)
data = {
«date»: » «,
«hour»: » «,
«concept»: » «,
«amount»: » «,
«income_expense»: » «,
})
else:
return ‘This is the POST method’
Remember that you must have imported the library from request before having carried out this process so that it works correctly for you. You do this this way: from flask import render_template, request.
In this way, you will have already used the methods get and post in an income and expense app and you can reproduce this process in another code program, if you need to.
What is the next step?
Now that you know the process for using methods get and post In an income and expense app, you can reproduce this process in code programs of a different kind, but with calls to web servers about which responses need to be recorded and their format.
If you want to know more about how programming with Python works and you want to be accompanied by a programming professional during the training process, we recommend you visit the syllabus of our Learn to Program from Scratch Full Stack Jr. Bootcamp and enroll. ¡Dare to sign up and become a professional programmer in less than a year!