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:
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:
Getting Started: The Basic Syntax
The fundamental syntax for using cURL is straightforward:
curl [options] <URL>
BashLet’s break this down:
Installing cURL
It is as simple as running a yum command in a shell SSH window.
yum install curl
BashOr 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:
curl https://www.example.com
BashPress 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:
curl -I https://www.example.com
Bash
curl -v https://www.example.com
Bash
curl -X POST https://api.example.com/users
Bash
curl -X POST -d "name=John&age=30" https://api.example.com/users
Bash
curl -H "Content-Type: application/json" -H "Authorization: Bearer your_token" -X POST -d '{"name": "Jane", "city": "London"}' https://api.example.com/users
BashReal-World Examples
Let’s look at a few practical scenarios:
Downloading a specific file
curl -O https://releases.ubuntu.com/22.04/ubuntu-22.04.2-desktop-amd64.iso
BashChecking the status code of a webpage
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
curl https://api.github.com/users/octocat
BashThis will return information about the GitHub user “octocat” in JSON format.
{
"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"
}
JSONTips for Using cURL Effectively
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!
Leave a Reply