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 url-query adds encoded parameters to any request

Curl
~/tips/tip-curl-url-query-append

curl --url-query 'q=hello world' \

Append query string parts to a URL that already has some, with encoding done for you.

-G with --data-urlencode turns data into a query string, but it also changes the request into a GET. --url-query only touches the URL, so it works with POST and PUT as well.

Each --url-query adds one part, URL encoded the same way --data-urlencode does it:

curl --url-query 'q=hello world' \
     --url-query 'lang=en' \
     'https://example.com/search?page=2'

The request goes to /search?page=2&q=hello+world&lang=en. curl keeps the query that was already in the URL and adds the new parts with an &. A value can also come from a file with name@file.

I find it most useful for an API that wants a token or a filter in the URL and a body at the same time:

curl --url-query "since=$DATE" --json @item.json https://api.example.com/items

If a value is already encoded, put a + in front of it and curl adds it untouched. Be careful with that one: +raw=a&b really sends two parameters, because the & stays as it is. The option needs curl 8.8 or newer, so older distributions may not have it yet. Check with curl --help all | grep url-query.

Further reading: curl man page

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