curl: what it is

Configurare noua (How To)

Situatie

curl is a command-line tool for transferring data with URLs using many protocols (HTTP, HTTPS, FTP, SFTP, etc.). It’s designed for flexible HTTP interactions (requests, headers, methods, authentication, forms, uploads, downloads) and scripting.

Solutie

Basic usage

  • Download a URL to stdout:
    curl https://example.com
  • Save output to a file:
    curl -o file.html https://example.com
  • Save with remote filename:
    curl -O https://example.com/file.zip

HTTP methods

  • GET (default):
    curl https://api.example.com/resource
  • POST with form data:
    curl -d "name=alice&age=30" https://example.com/form
  • POST JSON:
    curl -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/items
  • PUT:
    curl -X PUT -d '{"name":"new"}' -H "Content-Type: application/json" https://api.example.com/items/1
  • DELETE:
    curl -X DELETE https://api.example.com/items/1

Headers and authentication

  • Add custom header:
    curl -H "Authorization: Bearer TOKEN" https://api.example.com
  • Basic auth:
    curl -u username:password https://example.com/protected
  • Send multiple headers:
    curl -H "Accept: application/json" -H "X-Api-Version: 2" https://api.example.com

Files and uploads

  • Upload a file with multipart/form-data:
    curl -F "file=@/path/to/file.jpg" https://example.com/upload
  • Upload raw data from file:
    curl --data-binary @local.bin https://example.com/put

Follow redirects, show headers, verbose

  • Follow redirects:
    curl -L http://short.url
  • Show response headers:
    curl -i https://example.com
  • Show only headers:
    curl -I https://example.com
  • Verbose for debugging:
    curl -v https://example.com

Timeouts, retries, rate

  • Set max time:
    curl --max-time 30 https://example.com
  • Retry on transient errors:
    curl --retry 5 https://example.com

Cookies and sessions

  • Save cookies to file:
    curl -c cookies.txt -L https://example.com/login
  • Send cookies from file:
    curl -b cookies.txt https://example.com/dashboard

Scripting and output control

  • Silent mode (no progress):
    curl -s https://example.com
  • Silent but show errors:
    curl -sS https://example.com
  • Show HTTP status code only:
    curl -o /dev/null -s -w "%{http_code}\n" https://example.com

Examples

  • GET JSON and pretty-print (using jq):
    curl -s https://api.example.com/data | jq .
  • POST JSON with header:
    curl -s -H "Content-Type: application/json" -d '{"title":"Hi"}' https://api.example.com/posts
  • Download multiple files listed in urls.txt:
    xargs -n1 -P4 curl -O < urls.txt

Check version and help

  • Version:
    curl --version
  • Full help:
    curl --help

Tip solutie

Permanent

Voteaza

(7 din 7 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?