惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Martin Heinz's Blog

A Guide to Python's Weak References Using weakref Module Recent Docker BuildKit Features You're Missing Out On Modern Git Commands and Features You Should Be Using Everything You Can Do with Python's textwrap Module Monitoring Indoor Air Quality with Prometheus, Grafana and a CO2 Sensor Everything You Can Do with Python's bisect Module You Don't Need a Dedicated Cache Service - PostgreSQL as a Cache A Collection of Docker Images To Solve All Your Debugging Needs Weird Python "Features" That Might Catch You By Surprise Lessons Learned From Writing 100 Articles Debugging Crashes and Deadlocks in Python using PyStack Goodbye etcd, Hello PostgreSQL: Running Kubernetes with an SQL Database Remote Interactive Debugging of Python Applications Running in Kubernetes The Right Way to Run Shell Commands From Python Real Multithreading is Coming to Python - Learn How You Can Use It Now Python's Missing Batteries: Essential Libraries You're Missing Out On Kubernetes-Native Synthetic Monitoring with Kuberhealthy Make Your CLI Demos a Breeze with Zero Stress and Zero Mistakes Reduce - The Power of a Single Python Function Why I Will Never Use Alpine Linux Ever Again Cgroups - Deep Dive into Resource Management in Kubernetes Dictionary Dispatch Pattern in Python Boost Your Python Application Performance using Continuous Profiling Lazy Evaluation Using Recursive Python Generators Python Magic Methods You Haven't Heard About Getting Started with Mastodon API in Python Backup-and-Restore of Containers with Kubernetes Checkpointing API Getting Started with Google APIs in Python Python CLI Tricks That Don't Require Any Code Whatsoever All The Ways To Introspect Python Objects at Runtime What is Python's "self" Argument, Anyway? Python List Comprehensions Are More Powerful Than You Might Think You Should Be Using Python's Walrus Operator - Here's Why Recipes and Tricks for Effective Structural Pattern Matching in Python It's Time to Say Goodbye to These Obsolete Python Libraries Advanced Features of Kubernetes' Horizontal Pod Autoscaler Data and System Visualization Tools That Will Boost Your Productivity Stop Messing with Kubernetes Finalizers Automate All the Boring Kubernetes Operations with Python End-to-End Monitoring with Grafana Cloud with Minimal Effort Bitly | bit.ly/3JLmSgA Bitly | bit.ly/3uETfbi Ultimate CI Pipeline for All of Your Python Projects Bitly | bit.ly/3M30D82 Bitly | bit.ly/3oMJ6qR Bitly | bit.ly/3IRD7IK Bitly | bit.ly/3A3B69t Profiling and Analyzing Performance of Python Programs Bitly | bit.ly/30uviIM Bitly | bit.ly/3E1X2mw Bitly | bit.ly/3Dv7JxP Bitly | bit.ly/3GG1BEz Bitly | bit.ly/3lLavs4 Bitly | bit.ly/39TqP3m Bitly | bit.ly/3A5Mpx8 Bitly | bit.ly/3kGwPl4 Bitly | bit.ly/3iHtulU Bitly | bit.ly/3xGjtKS Bitly | bit.ly/3h8DZg0 Bitly | bit.ly/2RQn1dG Bitly | bit.ly/3p2B5wW Bitly | bit.ly/3tULpb0 Bitly | bit.ly/2PHVudx Bitly | bit.ly/3uPtnb0 Bitly | bit.ly/3dg3QR9 Bitly | bit.ly/3qHtSkZ Bitly | bit.ly/3kIkTPr Bitly | bit.ly/3qlRAUN Bitly | bit.ly/3pCUJ26 Hardening Docker and Kubernetes with seccomp Bitly | bit.ly/34ZhIMt Bitly | bit.ly/3qSO7h0 Bitly | bit.ly/3muGLOk Bitly | bit.ly/35xN79v Bitly | bit.ly/3mLGshK Bitly | bit.ly/2IvkGQl Bitly | bit.ly/2Sk1KFK Bitly | bit.ly/3iCNIL6 Saving Your Linux Machine from Certain Death New Features in Python 3.9 You Should Know About Deploy Any Python Project to Kubernetes Analyzing Docker Image Security Recursive SQL Queries with PostgreSQL Automating Every Aspect of Your Python Project Tour of Python Itertools Implementing 2D Physics in Javascript Ultimate Setup for Your Next Python Project Making Python Programs Blazingly Fast Security and Cryptography Mistakes You Are Probably Doing All The Time Going Serverless with OpenFaaS and Golang - Building Optimized Templates Going Serverless with OpenFaaS and Golang - The Ultimate Setup and Workflow Setting Up Swagger Docs for Golang API Building RESTful APIs in Golang Pytest Features, That You Need in Your (Testing) Life Setting up GitHub Package Registry with Docker and Golang Ultimate Setup for Your Next Golang Project Python Tips and Trick, You Haven't Already Seen, Part 2. Tricks for Postgres and Docker that will make your life easier Getting The Most Out of Reading Books - Reading The "Professional Way" Python Tips and Trick, You Haven't Already Seen
10 Examples Why cURL is an Awesome CLI Tool
Martin · 2024-08-01 · via Martin Heinz's Blog

Whether you're developer, DevOps engineer, SysAdmin, QA or in any other technical role, you're surely familiar with cURL - the command line tool and library for transferring data with URLs (as described in docs).

Most of the time however, we all really only use curl for simple tasks, such downloading a file or checking if website is accessible, yet there's some much more curl can do!

And in this article we will go through exactly those cool examples and tricks to showcase why curl is awesome and underappreciated tool...

Globbing

First up is globbing, allowing us to make multiple requests with a single curl command:


curl -s "https://jsonplaceholder.typicode.com/users/[1-3]" | jq -s .
curl -s "https://jsonplaceholder.typicode.com/users/[0-10:2]" | jq -s .

curl -s "https://jsonplaceholder.typicode.com/photos/{1,6,35}" | jq -s .

curl -s "https://jsonplaceholder.typicode.com/users/[1-3]" -o "file_#1.json"

The first two commands show how we can run a range of requests - first command produces requests to .../users/1, .../users/2, and .../users/3, while the other uses a step option, which gives us 2, 4, 6, 8 and 10. Considering that these particular requests return JSON, we also combine it with jq ... with the -s (slurp) operator, which joins responses of individual requests into single array.

Third examples uses list of specific numbers instead of range - this also works for characters and words - for example, we could use globbing to make requests with multiple protocols: {http,https}://...

The final example combines globbing with output variables, where the #1 variable in filename refers to the range [1-3]. This will produce file_1.json, file_2.json and file_3.json.

Configuration Files

Most of the time, when using curl we probably want to pass in same commandline options, such as proxy settings, request timeouts, headers, etc. That's where curl configuration file named .curlrc might come in handy:


# ~/.curlrc

# some headers
-H "Upgrade-Insecure-Requests: 1"
-H "Accept-Language: en-US,en;q=0.8"

# follow redirects
--location

It's just a text file, where each line represents one option that will be passed to curl. It's read automatically from ~/.curlrc, so you don't need any extra flags, but you can use -K to override or specify different location, e.g.:


curl -K .curlrc https://google.com

Similarly to flags and options, one might also want to pass in credentials. This can be done with --user option, but that will leave the credentials in shell history, so instead we can utilize .netrc file which curl supports:


# ~/.netrc
machine https://authenticationtest.com/HTTPAuth/
login user
password pass

The format includes machine (the URL), login and password - they can be on single line or as shown above, and we can also have multiple of them in single file. To use it, just pass it to curl like:


curl --netrc-file .netrc https://authenticationtest.com/HTTPAuth/

Parallel Requests

We've already talked about ranges of request in globbing section, but what about parallelization? Well, curl can do that too:


curl -I --parallel --parallel-immediate --parallel-max 3 --config websites.txt

curl -I --parallel --parallel-immediate --parallel-max 3 stackoverflow.com google.com example.com

All we need to do, is add --parallel (or -Z) and curl will open up to 50 parallel connections (can be changed with --parallel-max N). Also notice how we're supplying the URLs - first option is --config and a textfile that would look like:


url = "stackoverflow.com"
url = "google.com"
url = "example.com"

Other option is just putting all the URL on commandline. Both of these options also works with non-parallel request!

Formatting & Variables

curl can output a lot of stuff, and sometimes it can be overwhelming, verbose and unnecessary. Luckily, we can use output formatting to only print the things that we're interested in:


curl --silent --output /dev/null --show-error -w @format.txt http://example.com/

# Type: text/html; charset=UTF-8
# Code: 200
#
# From 8.1.0:
# Scheme: http
# Host: example.com
# Port: 80
#
# Read header content (v7.83.0):
# Server: Sat, 29 Jun 2024 13:01:30 GMT

We do so using the -w option and by passing in a format file. To produce output such as above, we can use:


# format.txt
Type: %{content_type}\nCode: %{response_code}\n\n

From 8.1.0:\n\n

Scheme: %{url.scheme}\n
Host: %{url.host}\n
Port: %{url.port}\n

Read header content (v7.83.0):\n
%header{date}

Each variable is enclosed in %{...}. They can be either simple ones like response_code or part of url.<NAME> which refer to URL components, such as host or port. Finally, we can also output response headers with %header{HEADER_NAME} variables.

One of the many nice use cases for formatting is measuring request/response time, which can be done with following format:


# format.txt
     time_namelookup:  %{time_namelookup}s\n
        time_connect:  %{time_connect}s\n
     time_appconnect:  %{time_appconnect}s\n
    time_pretransfer:  %{time_pretransfer}s\n
       time_redirect:  %{time_redirect}s\n
  time_starttransfer:  %{time_starttransfer}s\n
                     ----------\n
          time_total:  %{time_total}s\n

# Output:
     time_namelookup:  0.000765s
        time_connect:  0.111908s
     time_appconnect:  0.000000s
    time_pretransfer:  0.111967s
       time_redirect:  0.000000s
  time_starttransfer:  0.223373s
                     ----------
          time_total:  0.223992s

For full list of variables see docs.

Testing & Troubleshooting

Most common way to use curl is for (network) troubleshooting - oftentimes just making a request to particular URL will provide enough information, but there are more things we can do, for example we can force usage of specific local network interface:


ip link show
# ...
# 3: wlp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000

curl --interface wlp5s0 https://example.com

Similarly, specific DNS server can be forced:


curl --dns-ipv4-addr 1.1.1.1 https://example.com

Or we can test for timeout and capture exit code (exit codes):


curl --connect-timeout 30 --silent --output /dev/null \
  --show-error -w 'Total: %{time_total}s\n' http://google.com/ || EXIT_CODE=$?

if [ $EXIT_CODE = 28 ]
then
  echo "Cannot connect (Timeout)."
else
  echo "Can connect."
fi

This can be useful - for example - for testing if proxy server is working (with -x http://proxy.example.com:80).

Trurl

curl is more than just the CLI tool - the project includes libcurl as well as trurl which I want showcase here.

trurl is a dedicated tool for parsing URLs and is a sibling project to curl. It can be installed from source:


sudo apt-get install libcurl4-openssl-dev
git clone https://github.com/curl/trurl.git
cd trurl
make
# ...

And here are couple examples for using it:


trurl --url https://example.com/some/path/to/file.html --get '{path}'
# /some/path/to/file.html

trurl --url "https://example.com/?name=hello" --append query=key=value
# https://example.com/?name=hello&key=value

# Parse as JSON:
./trurl --url "https://example.com/?name=hello" --json
# [
#   {
#     "url": "https://example.com/?name=hello",
#     "parts": {
#       "scheme": "https",
#       "host": "example.com",
#       "path": "/",
#       "query": "name=hello"
#     },
#     "params": [
#       {
#         "key": "name",
#         "value": "hello"
#       }
#     ]
#   }
# ]

First one shows how we can extract URL component, here it's path, but can be e.g. url, scheme, user, password, options or host.

The second one uses the append feature, to add query parameter to the URL.

And final example shows the --json option which outputs the parsed URL as JSON, which is great for further processing.

There's lot more trurl can do - you can check out this video or manual (examples at the bottom).

Sending/Uploading Data

Most of the time we use curl to download or request data, but it can (obviously) also send data. POSTing data with curl is nothing new right?


curl -X POST "https://httpbin.org/post" -H "accept: application/json" --json '{"key": "value"}'

But sending JSON like this, having to alternate the single and double quotes gets annoying pretty quickly, but there's a better way:


jo -p key=value | curl -X POST "https://httpbin.org/post" -H "accept: application/json" --json @-

I think we're all familiar with parsing JSON output of curl with jq, but what about the opposite? - Above we use jo tool which easily creates JSON which we can then pass to curl using the --json option.

Naturally, the --json option can also take input from file, e.g. with --json @data.json.

Protocols

Last but not least here are protocols - usually we would only use HTTP or HTTPS, but curl supports a lot of protocols.

One I want to mention in particular is telnet, which is handy for testing if server listens on specific port, but what if you're on a server/machine where you don't have and can't install telnet? Simply use curl:


# Same as telnet example.com 1234
curl telnet://example.com:1234

Some of the more obscure (funny) protocol options are IMAP, POP3 and SMTP for emails, which means that you can read and send emails with curl. To read them:


curl --url "imaps://imap.gmail.com:993/Inbox;UID=1" --user "user@gmail.com:PASSWORD"

To make this work with GMail specifically, you would need to create app password which is less secure than normal password. If you actually want to try this, do check out also Gmail IMAP docs and these queries for inspiration.

For sending email, you can use:


curl smtp://mail.example.com \
  --mail-from me@example.com \
  --mail-rcpt someone@example.com \
  --upload-file message.txt \
  -u "me@example.com:PASSWORD"

Here the message.txt is the actual email, which needs to follow a specific format, check out this page for examples.

Conclusion

We're at the end, and I'm pretty sure that was at least 10 examples (I stopped counting). But honestly, this really barely scratches the surface - we haven't even touched the libcurl which is the big part of curl.

There's so much more you can do with curl so I recommend exploring both docs and https://everything.curl.dev/.