Utilizzo Base
Richieste Semplici
curl https://example.com # richiesta GET curl -o file.html https://url # salva su file curl -O https://url/file.tar.gz # salva con nome remoto curl -L https://url # segui redirect
Flag Comuni
-sModalità silenziosa (nessun progresso)
-SMostra errori in modalità silenziosa
-fFallisce silenziosamente su errori HTTP
-LSegui i redirect
-o fileScrivi output su file
-OSalva con nome file remoto
-C -Riprendi download interrotto
--max-time 30Timeout dopo 30 secondi
Metodi HTTP
GET e HEAD
curl https://api.example.com/users curl -I https://example.com # HEAD (solo header) curl -i https://example.com # includi header risposta
POST
curl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d '{"name":"Jo","email":"[email protected]"}'
PUT, PATCH e DELETE
curl -X PUT https://api.example.com/users/1 \ -d '{"name":"Updated"}' curl -X PATCH https://api.example.com/users/1 \ -d '{"email":"[email protected]"}' curl -X DELETE https://api.example.com/users/1
Header
Impostare gli Header
curl -H "Content-Type: application/json" URL curl -H "Accept: text/html" URL curl -H "X-Custom: value" URL curl -H "Header1: v1" -H "Header2: v2" URL
Header di Risposta
-iIncludi header di risposta nell'output
-IScarica solo gli header (HEAD)
-D fileSalva header di risposta su file
-w '%{http_code}'Stampa il codice di stato HTTP
Autenticazione
Auth Basic e Token
curl -u user:pass https://api.example.com curl -H "Authorization: Bearer TOKEN" URL curl -u user:pass --digest URL curl --negotiate -u : URL # Kerberos/SPNEGO
Metodi di Autenticazione
-u user:passAutenticazione Basic
--digestHTTP Digest auth
--negotiateAutenticazione Kerberos/SPNEGO
--ntlmAutenticazione NTLM
-nUsa credenziali ~/.netrc
Dati e Form
Invio Dati
curl -d "key=val&key2=val2" URL # form urlencoded curl -d @data.json URL # dati da file curl --data-raw '{"raw":"json"}' URL curl --data-urlencode "q=hello world" URL
Upload File
curl -F "[email protected]" URL curl -F "[email protected];type=application/pdf" URL curl -F "field=value" -F "[email protected]" URL
Multipart vs URL-Encoded
-dapplication/x-www-form-urlencoded
-Fmultipart/form-data
--jsonScorciatoia: imposta Content-Type + Accept a JSON
-T fileCarica file via PUT
SSL/TLS
Opzioni Certificato
curl --cacert ca.pem URL # bundle CA personalizzato curl --cert client.pem URL # certificato client curl --cert client.pem --key key.pem URL curl -k URL # salta verifica TLS (solo sviluppo)
Flag TLS
-k / --insecureSalta la verifica del certificato TLS
--cacert fileUsa certificato CA personalizzato
--cert fileCertificato client
--key fileChiave privata client
--tlsv1.2Forza TLS minimo 1.2
--tlsv1.3Forza TLS minimo 1.3
Output e Debugging
Verbose e Trace
curl -v URL # output verbose curl --trace dump.txt URL # trace completa su file curl --trace-ascii - URL # trace su stdout curl -w "\n%{http_code}\n" URL # formato output personalizzato
Variabili Write-Out
%{http_code}Codice di stato risposta HTTP
%{time_total}Tempo totale in secondi
%{time_connect}Tempo per stabilire la connessione
%{size_download}Byte scaricati
%{speed_download}Velocità media di download
%{redirect_url}URL di redirect (se presente)
%{ssl_verify_result}Risultato verifica SSL (0 = OK)
Esempio Write-Out
curl -s -o /dev/null -w \ "code: %{http_code}\ntime: %{time_total}s\n" \ https://example.com
Pattern Comuni
Workflow API
# GET JSON e pipe a jq curl -s https://api.example.com/data | jq '.items[]' # POST JSON con auth curl -s -H "Authorization: Bearer $TOKEN" \ --json '{"key":"val"}' https://api.example.com
Pattern di Download
# Download con barra progresso curl -# -O https://releases.example.com/v2.tar.gz # Riprendi download interrotto curl -C - -O https://releases.example.com/v2.tar.gz # Download di file multipli curl -O https://url/file1 -O https://url/file2
Helper per Scripting
# Verifica se l'URL è raggiungibile curl -sf -o /dev/null https://example.com && echo OK # Salva e riusa cookie curl -c cookies.txt -b cookies.txt URL # Limita la velocità della richiesta curl --limit-rate 100k URL
Proxy e Rete
Impostazioni Proxy
curl -x http://proxy:8080 URL curl -x socks5://proxy:1080 URL curl --proxy-user user:pass -x http://proxy:8080 URL curl --noproxy "*.local,localhost" URL
DNS e Resolve
--resolve host:port:addrForza risoluzione DNS a addr
--dns-servers 8.8.8.8Usa server DNS personalizzato
--interface eth0Usa interfaccia di rete specifica
-4 / -6Forza IPv4 / IPv6
Config e Avanzato
File di Configurazione
# ~/.curlrc — opzioni predefinite --silent --location --max-time 30 # Usa file di configurazione esplicitamente curl -K myconfig.txt URL
Flag Utili
--retry 3Riprova in caso di errori transitori
--retry-delay 2Attesa tra i tentativi (secondi)
--compressedRichiede e decomprime gzip/br
--limit-rate 100kLimita la velocità di trasferimento
-ZTrasferimenti paralleli (curl 7.66+)
--create-dirsCrea directory del percorso per -o