ProstDev ProstDev
Tutorials Mar 30, 2021 · 4 min read

The Power of cURL

cURL is a command-line tool used to transfer files and invoke APIs over protocols like HTTP, HTTPS, and FTP. Learn how to send GET, query params, URI params, headers, and POST requests, plus how to print output.

By Whitney Akinola
The Power of cURL· Part 1 of 2
  1. 1.The Power of cURL
  2. 2.The Power of cURL - Part II
The Power of cURL

Advanced Rest Client, Postman and SoapUI are top API clients you may have used while building or testing API integrations. These applications are useful. However there are cases where invoking the API from the command line is advantageous and is made possible through cURL.

What is cURL?

cURL is a tool used to transfer files. The cURL command can be used inside scripts or from the command line. cURL provides support for common protocols like HTTP, HTTPS, FTP and much more.

This article will focus on using the cURL command to invoke integrations that use HTTP. The cURL commands provided in this article will be demonstrated by invoking a sample API built in Mule 4, running on my local machine. Let’s get started!

How do I start?

Open your command prompt (Windows) or terminal (Mac/Linux). Run

curl

Git Bash terminal where running curl prints "try 'curl --help' for more information"

Look for the following output:

curl: try 'curl --help' for more information

This is a good indicator that cURL is installed on your local machine. If not, you will need to do a fresh install of cURL. Download cURL here.

cURL commands typically use the following syntax:

curl [options] [url]

Need help?

Use the —help option to provide a list of options.

curl --help

curl --help output listing common options like --data, --header, --output, and --verbose

Use the —manual option to reference the “man” page (or manual).

curl --manual

curl --manual page showing the cURL name, synopsis, and description of supported protocols

Note

Since I am using a Windows machine, I often use the command line from a Git Bash terminal.

Demo Time

The remainder of this article will focus on HTTP. All demonstrated cURL command examples will be HTTP requests (unless otherwise specified) and invoked against a simple Mule API running on my local machine. The API takes in the HTTP request and provides a breakdown of the request in the response.

REQUEST OPTION: To create an HTTP/HTTPS request. Use the —request option.

SYNTAX:

curl --request <method> '<url>'

Note

URL will include the protocol (http/https).

GET

GET requests can be written in two formats:

  1. Short-hand

SYNTAX:

curl '<url>'

EX:

curl 'http://localhost:8081/curl/helloget'
  1. Long-hand

SYNTAX:

curl --request GET '<url>'

EX:

curl --request GET 'http://localhost:8081/curl/helloget'

curl GET request to /curl/helloget and the JSON response echoing the request method and path

QUERY PARAMS

SYNTAX:

curl --request <method> '<url>?<queryParam1>=<value1>...&<queryParamN>=<valueN>'

EX:

curl --request GET 'http://localhost:8081/curl/helloget?firstname=Daffy&lastname=Duck'

curl GET with firstname and lastname query params echoed back in the response queryParams

URI PARAMS

SYNTAX:

 curl --request <method> '<url>/<uriParam>'

EX:

 curl --request GET 'http://localhost:8081/curl/helloget/1'

curl GET to /curl/helloget/1 with the uriParams id of 1 echoed in the response

HEADERS

SYNTAX:

curl --request <method> '<url>'  \
    --header '<key>:<value>'

Single Header

EX:

curl --request GET 'http://localhost:8081/curl/helloget' \
    --header 'Authorization: Bearer xybaneoovganfZbe'

curl GET with a single Authorization Bearer header echoed in the response headers

Multiple Headers

EX:

curl --request GET 'http://localhost:8081/curl/helloget' \
    --header 'client_id: bond' \
    --header 'client_secret: 0007'

curl GET with client_id and client_secret headers echoed in the response headers

POST

SYNTAX:

curl --request <method> '<url>' \
    --header 'Content-Type: <content-type>' \
    --data-raw '<data>'

EX:

curl --request POST 'http://localhost:8081/curl/hellopost' \
    --header 'Content-Type: application/json' \
    --data-raw '{ "RQ" : {"message" : "hello prostdev readers"}}'

curl POST to /curl/hellopost with a JSON body echoed back in the response requestBody

Interested in saving the output of your API invocation? Use the —output option.

SYNTAX:

curl --request <method> <url> --output output.txt

EX:

curl --request GET 'http://localhost:8081/curl/helloget'\ 
    --output output.txt

curl GET request using --output output.txt to save the response to a file

The saved output.txt opened in Notepad showing the JSON response from the curl request

Lessons Learned

  • Although quotes are not required around the URL, when sending requests with query parameters make sure you wrap requests around quotes. Without the quotes, I noticed that cURL dropped the query parameters.
  • Wrap your request URL in “double-quotes” if using command prompt and ‘single-quotes’ if using command line terminal. I found issues with ‘’ and “” between command prompt and command line.
  • Multi-line in command prompt is different from multi-line on command line.
    • Add “\” at the end of the line to enter a new line on a multi-line request from the command line.
    • Add “^” at the end of the line to enter a new line on a multi-line request from the command prompt.

Conclusion

This post invoked an API through HTTP. However, most of the time we invoke APIs over HTTPS. If you are interested in an HTTPS version of this post, please let us know through a comment.

If you would like to learn more about cURL, check out the cURL documentation here.

Lastly, I am Whitney Akinola. I’m a fellow MuleSoft Developer and content creator. Feel free to check out my technical content here.

FAQs

Frequently asked questions about this post.

  • How do I check whether cURL is already installed?

    Open your command prompt on Windows or terminal on Mac/Linux and run curl. If you see the output curl: try 'curl --help' for more information, that is a good indicator cURL is installed on your machine. If not, you can do a fresh install from https://curl.se/download.html .

  • What's the difference between the short-hand and long-hand way to send a GET request?

    Both produce a GET request. The short-hand form is just curl '<url>', while the long-hand form spells out the method explicitly as curl --request GET '<url>'.

  • How do I send query parameters with cURL?

    Append them to the URL after a ?, separating multiple pairs with &, as in curl --request GET 'http://localhost:8081/curl/helloget?firstname=Daffy&lastname=Duck'. The author notes you should wrap requests with query parameters in quotes, because without the quotes cURL dropped the query parameters.

  • How do I add headers to a cURL request?

    Use the --header option with a '<key>:<value>' pair, and repeat --header once per header for multiple headers, for example a single --header 'Authorization: Bearer xybaneoovganfZbe' or two lines --header 'client_id: bond' and --header 'client_secret: 0007'.

  • How do I save the output of a cURL request to a file?

    Use the --output option followed by a filename, as in curl --request GET 'http://localhost:8081/curl/helloget' --output output.txt, which saves the response into that file.

  • How do I write a multi-line cURL command?

    It differs by shell: add \ at the end of the line to continue onto a new line from the command line terminal, and add ^ at the end of the line to do the same from the Windows command prompt.

More from this series

The Power of cURL· Part 1 of 2
  1. 1.The Power of cURL
  2. 2.The Power of cURL - Part II
Search

Loading search…