M81

Welcoming the IBM Community

cURL

Your Guide to Using cURL

5
(3)

At the two recent i-UG (the largest IBM i User Group in the UK), I gave a presentation about three great open-source utilities on the IBM i.

Those being:

cURL
LFTP
rSync

Having just checked, I have written about the last two on PowerWire, so for completeness, I must write about cURL. A very powerful utility, so what is cURL?

What is cURL?

In the world of web development, system administration, and even just general internet tinkering, there’s a humble yet incredibly powerful tool that often flies under the radar for beginners: cURL.

Don’t let its command-line interface intimidate you.

Once you grasp the basics, cURL will become an indispensable part of your toolkit for interacting with web servers.

At its core, cURL is a command-line tool for transferring data with URLs.

Think of it as a non-graphical web browser. Instead of clicking buttons and navigating pages, you tell cURL exactly what you want to do using commands and options.

This makes it incredibly versatile for tasks like:

Downloading files: From simple images to entire websites
Making API requests: Interacting with web services to retrieve or send data
Testing APIs: Quickly checking the functionality and responses of your own or third-party APIs
Debugging web issues: Inspecting headers, cookies and the raw content of web pages
Automating tasks: Integrating web interactions into scripts

Getting Started: The Basic Syntax

The fundamental syntax for using cURL is straightforward:

Bash

curl [options] <URL>
Bash

Let’s break this down:

curl: This is the command itself, telling your system you want to use the cURL tool.
[options]: These are flags & parameters that modify cURL’s behaviour. There are many options available, giving you fine-grained control over your requests.
<URL>: This is the target web address you want to interact with.

Installing cURL

It is as simple as running a yum command in a shell SSH window.

Bash

yum install curl
Bash

Or by taking the Open-Source package management option off the main ACS window.

Your First cURL Command: Fetching a Webpage

Let’s try a simple example. To fetch the content of a website (like this article!), open a SSH terminal or command prompt and type:

Bash

curl https://www.example.com
Bash

Press Enter, and you’ll see the HTML source code of example.com printed directly in your terminal.

Congratulations, you’ve just made your first cURL request, how easy was that!

Essential cURL Options You Should Know

While simply fetching a webpage is a good start, the real power of cURL lies in its options, this is the same as passing parameters to a RPG program.

Here are a few of the most commonly used ones:

-o <filename> or -O (uppercase O): Save the output to a file.
-I (uppercase i): Get only the HTTP headers. This is useful for checking server status, content type & other metadata without downloading the entire content.
Bash

curl -I https://www.example.com
Bash
-v (verbose): Show more information about the request and response. Invaluable for debugging, as it displays details like the HTTP request sent, headers received & connection information.
Bash

curl -v https://www.example.com
Bash
-X <method>: Specify the HTTP request method. By default, cURL uses GET. You can use other methods like POST, PUT, DELETE, etc., when interacting with APIs.
Bash

curl -X POST https://api.example.com/users
Bash
-d <data> or –data <data>: Send data in the request body (usually for POST requests). This is how you send information to a server, like form data.
Bash

curl -X POST -d "name=John&age=30" https://api.example.com/users
Bash
-H <header>: Add custom headers to the request. This is often required when interacting with APIs for authentication or specifying content types.
Bash

curl -H "Content-Type: application/json" -H "Authorization: Bearer your_token" -X POST -d '{"name": "Jane", "city": "London"}' https://api.example.com/users
Bash

Real-World Examples

Let’s look at a few practical scenarios:

Downloading a specific file

Bash

curl -O https://releases.ubuntu.com/22.04/ubuntu-22.04.2-desktop-amd64.iso
Bash

Checking the status code of a webpage

Bash

curl -I https://httpbin.org/status/200
Bash

(You should see a 200 OK status in the headers)

Making a simple GET request to an API

Bash

curl https://api.github.com/users/octocat
Bash

This will return information about the GitHub user “octocat” in JSON format.

JSON

{
  "login": "octocat",
  "id": 583231,
  "node_id": "MDQ6VXNlcjU4MzIzMQ==",
  "avatar_url": "https://avatars.githubusercontent.com/u/583231?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/octocat",
  "html_url": "https://github.com/octocat",
  "followers_url": "https://api.github.com/users/octocat/followers",
  "following_url": "https://api.github.com/users/octocat/following{/other_user}",
  "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
  "organizations_url": "https://api.github.com/users/octocat/orgs",
  "repos_url": "https://api.github.com/users/octocat/repos",
  "events_url": "https://api.github.com/users/octocat/events{/privacy}",
  "received_events_url": "https://api.github.com/users/octocat/received_events",
  "type": "User",
  "user_view_type": "public",
  "site_admin": false,
  "name": "The Octocat",
  "company": "@github",
  "blog": "https://github.blog",
  "location": "San Francisco",
  "email": null,
  "hireable": null,
  "bio": null,
  "twitter_username": null,
  "public_repos": 8,
  "public_gists": 8,
  "followers": 17470,
  "following": 9,
  "created_at": "2011-01-25T18:44:36Z",
  "updated_at": "2025-03-22T11:26:21Z"
}

JSON

Tips for Using cURL Effectively

Read the manual: The man curl command in your terminal will open the comprehensive cURL manual, detailing every available option.
Experiment: Don’t be afraid to try different options and URLs to see how cURL behaves.
Use online resources: Numerous websites and tutorials offer more in-depth explanations and examples of cURL usage.
Combine with other tools: cURL can be piped/combined with other command-line tools like grep, jq & sed for powerful data manipulation.

Conclusion

cURL might seem daunting at first glance, but its versatility and power make it an invaluable tool for anyone working with the web. By understanding the basic syntax and exploring its many options, you can unlock a whole new level of control over how you interact with web servers and APIs.

So, open your SSH terminal, start experimenting, and unleash the power of the command line with cURL!

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 3

No votes so far! Be the first to rate this post.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *