How to Test APIs and Web Apps on Linux
Table of Contents
Testing APIs and web applications is a crucial step in the development process. While there are many graphical tools available for this task, sometimes it is more convenient to use the command line interface. In this how-to guide, we will explore how to test APIs and web apps from the command line on Linux.
Using cURL
cURL is a command line tool that is used to transfer data from or to a server, using one of the many supported protocols, including HTTP, HTTPS, FTP, FTPS, SMTP, and more. One of the most common use cases for cURL is testing APIs and web apps.
Step 1: Open Terminal
Open the terminal by pressing Ctrl + Alt + T on your keyboard or by searching for it in the applications menu.
Step 2: Install cURL
On Debian/Ubuntu-based distros, run:
sudo apt install curl
On Fedora/RPM-based distros, run:
sudo dnf install curl
On Arch-based distros, run:
sudo pacman -S curl
Step 3: Send GET Request
The simplest way to test an API or web app is to send a GET request. To do this, run:
curl -X GET https://api.example.com/users
This command sends a GET request to the specified URL, which should return a list of users. You can also include headers and request parameters.
Step 4: Send POST Request
If you need to send data to the API or web app, you can use a POST request. To do this, run:
curl -X POST -d “name=value” http://example.com/api
Replace name and value with the data you want to send. This will send a POST request to the specified URL with the data in the body of the request.
Step 5: Send PUT Request
To update existing data in an API or web app, you can use a PUT request. To do this, run:
curl -X PUT -d “name=value” http://example.com/api/id
Replace id with the ID of the data you want to update. This will send a PUT request to the specified URL with the data in the body of the request.
Step 6: Send DELETE Request
To delete data from an API or web app, you can use a DELETE request. To do this, run:
curl -X DELETE http://example.com/api/id
Replace id with the ID of the data you want to delete. This will send a DELETE request to the specified URL.
Step 7: View Response Headers
If you want to view the response headers of a request, run:
curl -I http://example.com/api
This will send a HEAD request to the specified URL and display the response headers.
Step 8: Save Response
To save the response from a request to a file, run:
curl -o filename http://example.com/api
Replace filename with the name you want to give to the file. This will save the response to the specified file.
In conclusion, cURL is a powerful tool that can be used to test APIs and web apps from the command line on Linux. With the commands we’ve shown here, you can easily send GET, POST, PUT, and DELETE requests, view response headers, and save responses to files.
Using HTTPie
HTTPie is another command line tool that is used for testing APIs and web apps. It is similar to cURL but has a more user-friendly interface.
Step 1: Open Terminal
Open the terminal by pressing Ctrl + Alt + T on your keyboard or by searching for it in the applications menu.
Step 2: Install HTTPie
On Debian/Ubuntu-based distros:
sudo apt install httpie
On Fedora/RPM-based distros:
sudo dnf install httpie
On Arch-based distros:
sudo pacman -S httpie
Step 3: Test GET Request
To test a GET request, we can simply use the http command followed by the URL of the API or web app we want to test.
For example, to test the GET request for the GitHub API, run:
http https://api.github.com/users/github
This will return the user information for the GitHub user.
Step 4: Test POST Request
To test a POST request, we need to specify the data we want to send in the request. We can do this by using the -d flag followed by the data in JSON format.
For example, to test the POST request for the JSONPlaceholder API, run:
http POST https://jsonplaceholder.typicode.com/posts \
userId=1 \
title=”foo” \
body=”bar”
This will create a new post with the specified data.
Step 5: Test Authentication
To test authentication, we need to use the –auth flag followed by the authentication credentials.
For example, to test authentication for the GitHub API, run:
http –auth user:password https://api.github.com/user
This will return the user information for the authenticated user.
Step 6: Test Headers
To test headers, we can use the -h flag followed by the headers we want to send.
For example, to test headers for the GitHub API, run:
http https://api.github.com/user \
“Authorization: token <token>”
This will return the user information for the user authenticated with the specified token.
Step 7: Test Cookies
To test cookies, we can use the –session flag followed by the name of the cookie session.
For example, to test cookies for the JSONPlaceholder API, run:
http –session=session POST https://jsonplaceholder.typicode.com/posts \
userId=1 \
title=”foo” \
body=”bar”
This will create a new post with the specified data and store the session cookies in the specified session.
In conclusion, HTTPie is a powerful command line tool for testing APIs and web apps from the command line on Linux. With its simple syntax and powerful features, it is a great alternative to using a web browser or a complex testing framework.
Using wget
wget is a command line tool that is used for retrieving files from the web. While it is not specifically designed for testing APIs and web apps, it can be used for this purpose in some cases. To test a REST API with wget, run:
wget -qO- https://api.example.com/users
This command sends a GET request to the specified URL and prints the response to the console. You can also include headers and request parameters. For example:
wget –header=’Content-Type: application/json’ –post-data='{“name”: “John”, “age”: 30}’ https://api.example.com/users
This command sends a POST request to create a new user with the specified name and age.
Conclusion
In conclusion, testing APIs and web apps from the command line on Linux can be a powerful and efficient way to ensure that your application is working as expected. The tools we have discussed here, cURL, HTTPie, and wget, are just a few examples of the many options available. Choose the tool that best fits your needs and get testing!