What is cURL and how does it relate to APIs?

Time:2022-8-5

cURLi.e. clientURL, representingClient URL, is a command-line tool that developers use to interact with the server. For example: the most common is to send data to the specified server url address through cURL. cURL supports many differentprotocol, including HTTP and HTTPS, and can be executed on different operating system platforms. You only need a terminal with a network connection that can run the command line to run curl commands.

The most basic usage format of curl is:curl http://example.com. i.e. curlcommand followed by the URL and it will returnThe html source code for example.com.

What is curl used for?

  • It is highly portable and compatible with almost all operating systems and connected devices.
  • Very handy to test end devices to check if they are working properly.
  • It can provide detailed debugging information, such as providing details of data sent/received.

Send API request

We can send API requests using curl. Each request usually consists of four main parts:

  • Oneendpoint, which is the url address we send the request to.
  • HTTPmethod. The most common methods are GET, POST, PUT, and DELETE.

    • GETUsed to retrieve resources from the server. This can be files, data or images.
    • POSTUsed to send data to the server.
    • PUTCan be used to create or update resources. This can be used to create or update the contents of records or files in a database.
    • DELETEUsed to delete resources such as database records.
  • Headers, which contains metadata about the request, such as content-type, user-agent, etc.
  • Body, which is the message body and contains the data we want to send. Typically, used with the POST and PUT methods.

curl command options

There are over two hundred curl options. You can view the help documentation by executing `curl -h at the command line. The most common command options include:

  • -IReturn only HTTPS Header

    curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=<myapikey>&date=2020-01-01' -I

    This command will return headers such as date, content-type, etc.

    What is cURL and how does it relate to APIs?

  • -vis a detail option

    curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=$NASA_API_KEY&date=2020-01-01' -v

    This command will display all the details of what happens when you run the curl command, including connection information, headers, and any data returned. Here we also get the description of the image returned by the request along with the image url.

    What is cURL and how does it relate to APIs?

  • -oStore the output in a file

    curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=$NASA_API_KEY&date=2020-01-01' --output curloutput

Using curl with other CLI commands

You can use the output of other commands as input to the curl command, or vice versa.

For example, you can use curl and grep to see if a web page contains specific text.

Here is an example that uses curl to request the NASA API, and uses python3 to extract the image link from the data returned by the request, and display it in the previewer:

curl --request GET "https://api.nasa.gov/planetary/apod?api_key=$NASA_API_KEY&date=2020-01-01" -s | python3 -c "import sys, json; print(json.load(sys.stdin)['url'])" | xargs curl -o NASApicture.jpg && open -a Preview NASApicture.jpg

In this example, we use curl to make a GET request to the Nasa API, which returns json data, and we extract the url of the image from the returned json data in a Python script fragment. Then we use the curl command to get the image and open it with the Preview image preview program on the Mac.

What is cURL and how does it relate to APIs?

Other useful tools for making API calls

You don't have to use the command linecurlto make API requests. You can also use a variety of other tools for API interaction, such as HTTPie, Postman, and Rest Client.

HTTPie

HTTPie is a command-line HTTP client, and its user-friendly interface is one of its major features. you can download it directlyonline version, very neat.

Postman

Postmanis a UI-based client for everything related to API development, and it's arguably one of the most popular.

You can generate and execute in PostmancurlOrder. to generatecurlcommand, you can enter the requested URL address and request parameters, and then click on the rightCode options:

What is cURL and how does it relate to APIs?

A box will appear, selectcurlto view the generatedcurlOrder.

What is cURL and how does it relate to APIs?

Postman gives you a history of all the requests you've built, even tagging them by date.

What is cURL and how does it relate to APIs?

Rest Client client in VS Code

Rest Clientfor VS Code is probably my favorite implementationcurlOne of the command tools. It's lightweight and has nice syntax highlighting. This is a very useful vs code plugin for you to quickly perform some curl requests in VS Code.

You just enter your curl command and the &quot;Send Request&quot; option will appear above.

What is cURL and how does it relate to APIs?

clicksend request, another tab will open with the response.

Summary and next steps

This article describes the basiccurlcommand and its most useful options. We've also covered tools to help you get started with cURL. You can now start using cURL to test your endpoints and troubleshoot your application.

Recommended Today

News|WebRTC M94 Update

WebRTC M94Currently released in Chrome Beta, it contains 1 new feature and more than 19 bug fixes, feature enhancements, stability and performance improvements. Welcome to followNetEase Yunxin Zhihu Institution Number, we will regularly translate WebRTC related content to help developers get the latest information and be at the forefront of the industry. 01. Public service […]