How to Test APIs and Web Apps on Linux

Testing APIs and web applications is a crucial step in the development process.

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!

Please Leave Feedback in the Comments

More to Explore

Why MALIBAL?

In a market saturated with well-known laptop brands, MALIBAL stands out as a unique choice for discerning users.

Read More »

Leave a Reply

Search

Type in a question or keyword below
Search

FAQ

Questions and Answers
Who is MALIBAL?

MALIBAL is an innovative technology company that produces high-performance, custom Windows laptops and Linux laptops for developers and content creators; mobile workstations for engineers, scientists, video editors, 3D modelers, and animators; and mobile servers for enterprise applications.

More

Why MALIBAL?

In a marketplace dominated by large corporations, MALIBAL offers a refreshing alternative that focuses on open-source support, the right to repair, hardware integration in the USA, personalized US-based customer support, and the unique combination of customization and high-performance technology in sleek, user-friendly designs.

More

Are all the parts soldered to the motherboard?

In many modern laptops, especially thin and light models (like Apple’s MacBook Air or Pro, Dell’s XPS 13, or many of Lenovo’s ThinkPad X1 Carbon models), the memory, storage, battery, and wireless module are often soldered directly onto the motherboard.

However, as part of our commitment to the Right to Repair Movement, the memory, storage, battery, and wireless module on all MALIBAL laptops are user-upgradeable or user-replaceable.

Can your laptops be charged or powered via USB-C?

All MALIBAL laptops can be powered via the Thunderbolt 4 port.

How do I track my order after it ships?​
You can view your tracking status via the order details page in your account.
How do I get a quote?

If you need a quote for any reason, e.g., to submit to accounting for approval, before ordering, simply add the laptop(s) you want to purchase to the Cart, then click Checkout, and on the checkout page, click Convert Cart to Quote. We will email you a PDF of the quote with a link to make payment once you are ready to complete the order.

How do I cancel an order?

You may cancel an order any time up until it ships. After it ships, you will not be able to cancel it, but will instead have to use our return policy in order to return the laptop for a refund. To cancel an order, simply open a sales ticket or sales chat and give the representative your order information, and they will cancel the order for you and send you confirmation via email and text.

How do I make a change to my order?

You may make changes to your order up until it ships. To modify an order, simply open a sales ticket or sales chat and give the representative your order information and specify which changes you want to make.

When is my order shipping?

If all parts are in stock, the average build time for laptops is 5-7 business days. This means laptops will ship 5-7 business days after the order is placed. If a component is backordered, it will say which parts are backordered in your order confirmation email.

To see the estimated delivery date of your laptop, please check your order confirmation email for the Estimated Delivery Date. You can also see this information on your My Account > Order Details page. The estimated delivery date is the build time (plus backorder delay, if applicable) plus the shipping time.

Once your laptop ships out, we will email you the tracking information. An adult will need to be at the address to sign for the package when it arrives.