Today, we will talk about an instrument that allows us to access our app directly from the console. It may seem unreal. How can we see HTML pages from the console? The answer is simple; we will observe the code instead of a rendered page. But the next questions is – why would we want to do it? The thing is – we actually don’t. Though, we can also use it to test API methods. It allows us to use the capabilities of our application for different platforms.
Curl
Curl is a command-line utility that allows us to execute requests and receive responses from web applications. It's worth noting the Curl purpose. If our service sends us HTML pages, it will be more convenient to observe responses through a browser to check how the web page looks, as Curl shows the plain text without highlighting. Although, when we test the applications that are designed for other programs (when our application works with other services, not users), it will be much easier to look at them using Curl.
Now, let's try to GET something!
GET requests
For this purpose, we will write a simple app that returns JSON data. It should be familiar to you:
from flask import Flask, jsonify
app = Flask('main')
@app.route('/')
def main_view():
return jsonify({'status': 200, 'message': "It's okay!"})
app.run()
Now, it's time to receive something interesting from our app. We can use Curl. This part is pretty simple — we type the name first and after that enter URL, or, in our case IP and port:
curl 127.0.0.1:5000
The output of this command looks like this:
By default, Curl sends a GET request.
POST requests
Now, let's figure out how we can use Curl to send POST requests. It's rather simple — we also need to write the module name and then use the -X argument. After this, we will specify the type of request that is sent to the server. In this case, it is a POST:
curl -X POST 127.0.0.1:5000
This command now will give the following result:
The server informed us that the POST method is not allowed for this page. It should not surprise you as we haven't specified it.
If we fix our code a little by importing a request and modifying the view function, we can see how it works.
from flask import Flask, jsonify, request
app = Flask('main')
@app.route('/', methods=['GET', 'POST'])
def main_view():
if request.method == 'GET':
return jsonify({'status': 200, 'message': "It's okay!"})
elif request.method == 'POST':
return jsonify({'status': 200, 'message': "This is POST!"})
In the view function, we have defined two branches: one for POST and one for GET. In addition, we have added POST to allowed methods. Now, when we run our app and enter the same command in the command line, we get a new answer:
Perfect! Now, we can test our web applications and see what they return by which URL. This can help a lot, for example, when we create view functions that return JSON with data. It will be quite easy to look at them and see whether everything is in order and we haven't messed up anything. Of course, there are other tools that might be used to test an API (and we’ll discuss them in other topics), but Curl is the simpliest and therefore most accessible one.
Conclusion
Today, we have learned about the common usage of Curl and how to receive data in JSON format from our web application. This is a useful skill for debugging and future application development. Don't slow down, and good luck with Python!