brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl -g stops brackets and braces in a URL from being read as a pattern

Curl
~/tips/tip-curl-globoff-brackets

curl -s 'https://example.com/api/items?ids[0]=4'

Send a URL with square brackets or curly braces exactly as you typed it.

curl treats [1-5] and {a,b} in a URL as a pattern that expands to several URLs. That is great for numbered files and confusing for APIs that use brackets in query strings.

Try a PHP style array parameter and curl complains:

curl -s 'https://example.com/api/items?ids[0]=4'
echo $?

That ends with exit code 3, a malformed URL, because [0] looks like a broken range. Braces are sneakier: ?id={42} is sent as ?id=42 without any warning. Add -g (long form --globoff) and curl leaves the URL alone:

curl -g 'https://example.com/api/items?ids[0]=4&ids[1]=7'

I’d put -g in any script that builds URLs from data I do not control, since a stray bracket in a search term would otherwise change the request.

The quotes still matter. Without them your shell may try to expand the brackets as a file pattern before curl even sees them. And once globbing is off, the numbered download trick with [01-10] no longer works in that command, so keep the two kinds of jobs apart. If a server is strict about URLs, you can also encode the brackets as %5B and %5D.

Further reading: curl man page, everything curl: URL globbing

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link