Forget Postman CURL is the only guy you need to Consume API

hipster' Santos
5 min readJul 30, 2024

Master curl at once instead of keep installing related-app like postman , insonia

The annoying part of using API-test application like postman and Insonia is that you have to install 'them not only this , they will consume memory in your machine , if you're like me ,terminal is lightweight and pretty fast when it comes to response time .

In this article I wanna show you how I use it and in combination with pipes and regular expressions to consume api directly in your terminal without installing anything application that could lagging your computer's performance .

Using curl to make HTTP requests is a powerful and flexible way to interact with web services.Below is a comprehensive guide that covers the basics for beginners, intermediate examples, and advanced use cases.

CURL a pan for "Client URL" did you new mostly typed like this cURL.

let's get start .let's comsuning api , we're gonna use http://numbersapi.com/ for testing

GET

Let’s start with making GET requests using cURL and explore different variations using the API "http://numbersapi.com/". This API provides trivia, math facts, and other details about numbers.

1. Basic GET Request

To get trivia about a specific number, we can use a simple GET request:


curl https://numbersapi.com/43
#output: 43 is the maximum number of cars participating in a NASCAR race in the Cup Series or Nationwide Series.%

2. GET Request with Different Categories

The Numbers API supports several categories, such as trivia, math, date, and year. Here’s how you can specify those categories:

Example: Math fact about a number:


curl numbersapi.com/43/math

#output 43 is a centered heptagonal number.%


#Example: Trivia about a specific year

curl http://numbersapi.com/2000/year

#output: 2000 is the year that a preliminary draft of genomes, as part of the Human Genome Project, is finished on June 26th.%

#fact about specific date

curl http://numbersapi.com/23/24
#Output: November 24th is the day in 1962 that the West Berlin branch of the Socialist Unity Party of Germany forms a separate party, the Socialist Unity Party of West Berlin.%

Let’s continue our cURL guide using a free movie API. One popular free API for movie data is The Open Movie Database (OMDb API). It provides information about movies, TV shows, and actors. You'll need an API key for this, which can be obtained for free by signing up on their website.

Base URL:

API Key:

Once you sign up, you’ll receive an API key. For this guide, I’ll use YOUR_API_KEY as a placeholder. Be sure to replace it with your actual key.

for this example I'll share with you my key for testing purposes

http://www.omdbapi.com/?i=tt3896198&apikey=915ad862

Example GET Request (Search for a movie):

curl "http://www.omdbapi.com/?apikey=YOUR_API_KEY&t=Inception"

#using a valida key

curl "http://www.omdbapi.com/?apikey=915ad862&t=Inception"

output:

{"Title":"Inception","Year":"2010","Rated":"PG-13","Released":"16 Jul 2010","Runtime":"148 min","Genre":"Action, Adventure, Sci-Fi","Director":"Christopher Nolan","Writer":"Christopher Nolan","Actors":"Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page","Plot":"A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O., but his tragic past may doom the project and his team to disaster.","Language":"English, Japanese, French","Country":"United States, United Kingdom","Awards":"Won 4 Oscars. 159 wins & 220 nominations total","Poster":"https://m.media-amazon.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.8/10"},{"Source":"Rotten Tomatoes","Value":"87%"},{"Source":"Metacritic","Value":"74/100"}],"Metascore":"74","imdbRating":"8.8","imdbVotes":"2,592,712","imdbID":"tt1375666","Type":"movie","DVD":"N/A","BoxOffice":"$292,587,330","Production":"N/A","Website":"N/A","Response":"True"}%

1. Search for a Movie by Title

This is a basic GET request where you specify the movie title using the t parameter.

Example:

curl "http://www.omdbapi.com/?apikey=915ad862&t=The%20Matrix"

Outuput:

{"Title":"The Matrix","Year":"1999","Rated":"R","Released":"31 Mar 1999","Runtime":"136 min","Genre":"Action, Sci-Fi","Director":"Lana Wachowski, Lilly Wachowski","Writer":"Lilly Wachowski, Lana Wachowski","Actors":"Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss","Plot":"When a beautiful stranger leads computer hacker Neo to a forbidding underworld, he discovers the shocking truth--the life he knows is the elaborate deception of an evil cyber-intelligence.","Language":"English","Country":"United States, Australia","Awards":"Won 4 Oscars. 42 wins & 52 nominations total","Poster":"https://m.media-amazon.com/images/M/MV5BNzQzOTk3OTAtNDQ0Zi00ZTVkLWI0MTEtMDllZjNkYzNjNTc4L2ltYWdlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.7/10"},{"Source":"Rotten Tomatoes","Value":"83%"},{"Source":"Metacritic","Value":"73/100"}],"Metascore":"73","imdbRating":"8.7","imdbVotes":"2,087,394","imdbID":"tt0133093","Type":"movie","DVD":"N/A","BoxOffice":"$172,076,928","Production":"N/A","Website":"N/A","Response":"True"}%

GET Request with Query Parameters

You can include additional query parameters to refine your search. For example, you can specify the type of result (movie, series, episode), year of release, etc.

Example: Get only movies from 1999 with the keyword “Matrix”


curl "http://www.omdbapi.com/?apikey=915ad862&s=matrix&type=movie&y=1999"

Output:

{
"Search": [
{
"Title": "The Matrix",
"Year": "1999",
"imdbID": "tt0133093",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BN..."
}
],
"totalResults": "1",
"Response": "True"
}

GET Request with Multiple Query Parameters

You can add more query parameters to filter the search results, such as genre or release date.

Example: Search for a movie, specifying the plot return type and year

curl "http://www.omdbapi.com/?apikey=915ad862&t=Inception&y=2010&plot=full"

GET with Headers

You can send headers along with your GET request using the -H option. For example, even though OMDb doesn’t require specific headers, you can simulate including authentication headers or others.

Example:

culr -H "Authorization: Bearer 915ad862" \
"http://www.omdbapi.com/?apikey=YOUR_API_KEY&t=Inception"

Handling Timeouts

If you want to set a timeout for the request to avoid it hanging indefinitely, use the --max-time option.

Example:

curl --max-time 5 "http://www.omdbapi.com/?apikey=YOUR_API_KEY&t=Inception"

Save the Response to a File

You can use the -o option to save the response to a file:

When you want to save the response of a cURL request to a file, you use the -o or -O flags.

Using -o (lowercase):

  • This allows you to specify the output filename.
  • Example: Save movie data from OMDb API to a file named inception.json.

Example:

curl -o movie_info.json "http://www.omdbapi.com/?apikey=YOUR_API_KEY&t=Inception"

# more example


curl -o matrix.json "http://www.omdbapi.com/?apikey=915ad862&t=The%20Matrix"

#output
# % Total % Received % Xferd Average Speed Time Time Time Current
# Dload Upload Total Spent Left Speed
# 100 1068 100 1068 0 0 1873 0 --:--:-- --:--:-- --:--:-- 1938

Using -O (uppercase):

  • This saves the response to a file using the server’s suggested filename (from the URL).

Example:


curl -O "http://example.com/somefile.json"

#more examples:

curl -O http://example.com/file1.txt -O http://example.com/file2.txt

Handling Timeouts

why you need handle timeous ? we'll answer it latter first learn the basic to understand why you need handle timeouts.

By default, cURL can wait indefinitely for a server response. You can control this using the --max-time, --connect-timeout, and --retry options.

--max-time:

  • Limits the total time the cURL request is allowed to take.

Example: Timeout the request after 10 seconds.

This will stop the request if it takes more than 10 seconds.

curl --max-time 10 "url_here"

--connect-timeout:

  • This limits the time cURL waits to establish a connection before timing out.

Example: If the connection isn’t established within 5 seconds, it will timeout.

curl --connnect-timeout 5 "url_here"

--retry:

  • Automatically retry failed requests (like network issues).

Example: Retry 3 times on failure.

curl --retry "url_here"

You can also combine these options for fine-tuning how cURL behaves under different network conditions.

Handling Headers

In cURL, headers allow you to pass additional information with the request. You can send and view request/response headers.

Custom Request Headers:

  • Use -H to include headers in the request.
  • Example: Send an Authorization Bearer token with your request.

--

--

hipster' Santos
hipster' Santos

Written by hipster' Santos

Fullstack developer , Distributed system engineer,Competitive programmer find at https://github.com/HipsterSantos

No responses yet