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 -L turns your POST into a GET after a redirect

Curl
~/tips/tip-curl-post301-keep-post

curl -L -d 'name=test' https://example.com/old

Keep a POST a POST when curl follows a 301, 302 or 303.

Post some data to an old URL with -L, and the server at the new address may see a GET without any body. That is curl copying what browsers do, and there are flags to turn it off.

Here /old answers with a 301 to /new:

curl -L -d 'name=test' https://example.com/old
curl -L --post301 -d 'name=test' https://example.com/old

The first command sends a POST to /old and then a GET to /new, and your data is gone. The second keeps the POST and the body on the second request. There are three flags, one per status code: --post301, --post302 and --post303.

The man page says it straight: the HTTP spec says a 301 should keep the method, but browsers switch to GET, so curl does the same by default. Status codes 307 and 308 always keep the method, which is why APIs that move endpoints often use 308.

This happens most with an http:// URL that redirects to https://. The symptom is a confusing error from the API about a missing body, while your command clearly has one. Before adding the flags, I would rather fix the URL in the script so there is no redirect at all. And do not reach for -X POST as the fix, because that forces POST on every hop, including ones that should be a GET.

Further reading: curl man page, MDN: HTTP redirections

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