Python Falcon - API Testing Tools



Falcon is a minimalistic framework suitable for developing APIs. An API is an interface between two applications. The API developer needs to test its functionality, reliability, stability, scalability, and performance etc. before releasing it for use in production environment.

Various API testing tools are available for this purpose. In this section, we shall learn how to use command line tools Curl and HTTPie, and a GUI tool called Postman.

cURL

cURL is an open source project that provides libcurl library and a command line tool called curl that enables transferring data using various protocols. More than 20 protocols including HTTP are supported. The acronym cURL stands for Client URL. The syntax for using Curl from command line is −

curl [options] [URL1, URL2,..]

The URL parameter consists of protocol dependent, one or more URL strings. The Curl command can be customized with various options. Some of the important command line options are as follows −

  • – X: Mention the request method. By default, Curl assumes GET to be the request method. To send POST, PUT or DELTETE requests, this option must be used. For example −

Curl –X DELETE http://localhost:8000/student/1
  • – H: This option is used to add headers in the request. For example −

Curl –H "Content-Type: application/json" -X GET
http://localhost:8000/students
  • – i: When this option is included in the command line, all the response headers are displayed. For example −

Curl –I –X DELETE http://localhost:8000/student/2
  • – d: To include data in the HTTP request for processing, we have to use this option, particularly when POST or PUT request is needed.

Curl –H "Content-Type: application/json" -X PUT -d
"{"""marks""":"""50"""}" http://localhost:8000/students/3

HTTPie

The HTTPie is a command line tool written in Python. It is said to be a "cURLlike tool for humans". It supports forms and file uploads and generates nicely formatted colorized terminal output. Its expressive and intuitive syntax makes it easier to use as compared to Curl.

Examples

  • GET request − http GET localhost:8000/students

  • POST request − http POST localhost:8000/students id=4 name="aaa" percent=50

  • PUT request − http PUT localhost:8000/students/2 id=3 name="Mathews" percent=55

  • DEETE request − http DELETE localhost:8000/students/2

Postman

Postman is a very popular API testing tool. It is a GUI app as against Curl and HTTPie. It is available in the form of a browser plugin as well as a desktop application. As the browser plugin doesn't accept requests for localhost based APIs, we need to download the desktop version from https://www.postman.com/downloads.

After completing the wizard based installation, start the Postman app and create a new request.

Python Falcon API1

The dropdown shows various HTTP request types to choose from.

Python Falcon API2

Enter http://localhost:8000/hello in the request URL field. The response pane on the right shows the result.

Python Falcon API3

We shall use the corresponding request types later when we test the Falcon API for CRUD operations on a SQLite database.

Advertisements