curl und wget sind essentielle Tools für HTTP-Requests auf der Kommandozeile. Sie ermöglichen Downloads, API-Tests und automatisierte Web-Interaktionen.
curl
Grundlagen
# Einfacher GET-Request
curl https://example.de
# Response-Header anzeigen
curl -I https://example.de
curl --head https://example.de
# Header und Body
curl -i https://example.de
# Nur HTTP-Status
curl -o /dev/null -s -w "%{http_code}\n" https://example.de
Datei herunterladen
# In Datei speichern
curl -o datei.zip https://example.de/datei.zip
# Originalen Dateinamen verwenden
curl -O https://example.de/datei.zip
# Fortschritt anzeigen
curl -O --progress-bar https://example.de/large.zip
# Download fortsetzen
curl -C - -O https://example.de/large.zip
HTTP-Methoden
# GET (Standard)
curl https://api.example.de/users
# POST
curl -X POST https://api.example.de/users
# PUT
curl -X PUT https://api.example.de/users/1
# DELETE
curl -X DELETE https://api.example.de/users/1
# PATCH
curl -X PATCH https://api.example.de/users/1
POST-Daten senden
# Form-Daten
curl -X POST -d "name=Max&email=max@example.de" https://api.example.de/users
# JSON-Daten
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"Max","email":"max@example.de"}' \
https://api.example.de/users
# Aus Datei
curl -X POST \
-H "Content-Type: application/json" \
-d @data.json \
https://api.example.de/users
# Datei hochladen
curl -X POST -F "file=@photo.jpg" https://api.example.de/upload
# Einzelner Header
curl -H "Authorization: Bearer token123" https://api.example.de
# Mehrere Header
curl -H "Authorization: Bearer token123" \
-H "Accept: application/json" \
-H "X-Custom-Header: value" \
https://api.example.de
# Content-Type
curl -H "Content-Type: application/json" ...
Authentifizierung
# Basic Auth
curl -u username:password https://api.example.de
# Bearer Token
curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..." https://api.example.de
# API-Key
curl -H "X-API-Key: your-api-key" https://api.example.de
SSL/TLS
# Zertifikat ignorieren (unsicher!)
curl -k https://self-signed.example.de
# Client-Zertifikat
curl --cert client.crt --key client.key https://secure.example.de
# CA-Bundle
curl --cacert /path/to/ca.crt https://example.de
Redirects
# Redirects folgen
curl -L https://example.de/redirect
# Max. Redirects
curl -L --max-redirs 5 https://example.de
Timeout
# Verbindungs-Timeout (Sekunden)
curl --connect-timeout 10 https://example.de
# Gesamt-Timeout
curl --max-time 30 https://example.de
Debugging
# Verbose
curl -v https://example.de
# Noch mehr Details
curl -vvv https://example.de
# Nur Timing
curl -o /dev/null -s -w "
DNS: %{time_namelookup}s
Connect: %{time_connect}s
TLS: %{time_appconnect}s
Start: %{time_starttransfer}s
Total: %{time_total}s
Size: %{size_download} bytes
Speed: %{speed_download} bytes/s
HTTP: %{http_code}
" https://example.de
Cookies
# Cookies speichern
curl -c cookies.txt https://example.de
# Cookies senden
curl -b cookies.txt https://example.de
# Beides
curl -c cookies.txt -b cookies.txt https://example.de
# Einzelnen Cookie
curl -b "session=abc123" https://example.de
Proxy
# HTTP Proxy
curl -x http://proxy:8080 https://example.de
# SOCKS5 Proxy
curl --socks5 localhost:9050 https://example.de
# Mit Auth
curl -x http://user:pass@proxy:8080 https://example.de
wget
Grundlagen
# Datei herunterladen
wget https://example.de/datei.zip
# Anderen Namen
wget -O neuername.zip https://example.de/datei.zip
# In Verzeichnis
wget -P /downloads/ https://example.de/datei.zip
# Hintergrund
wget -b https://example.de/large.zip
# Quiet
wget -q https://example.de/datei.zip
Website spiegeln
# Einfacher Mirror
wget --mirror https://example.de
# Mit Optionen
wget --mirror \
--convert-links \
--adjust-extension \
--page-requisites \
--no-parent \
https://example.de/docs/
# Kurzform
wget -mkEpnp https://example.de/docs/
Download fortsetzen
wget -c https://example.de/large.zip
Rekursiver Download
# Rekursiv mit Tiefe
wget -r -l 2 https://example.de
# Nur bestimmte Dateitypen
wget -r -A "*.pdf,*.doc" https://example.de
# Ohne bestimmte Dateien
wget -r -R "*.jpg,*.png" https://example.de
Bandbreite begrenzen
# Auf 1 MB/s begrenzen
wget --limit-rate=1M https://example.de/large.zip
Authentifizierung
# HTTP Auth
wget --user=username --password=password https://example.de
# FTP
wget --ftp-user=user --ftp-password=pass ftp://example.de/file
Liste von URLs
# Aus Datei
wget -i urls.txt
# urls.txt:
# https://example.de/file1.zip
# https://example.de/file2.zip
User-Agent
wget --user-agent="Mozilla/5.0 (compatible)" https://example.de
Vergleich curl vs wget
| Feature | curl | wget |
|---|
| HTTP-Methoden | Alle | GET, POST |
| JSON-Ausgabe | Ja | Nein |
| Website-Mirror | Nein | Ja |
| Rekursiv | Nein | Ja |
| Download fortsetzen | -C - | -c |
| Protokolle | Viele | HTTP, FTP |
| Scripting | Besser | Einfacher |
Praktische Beispiele
API testen
# GET mit JSON-Formatierung
curl -s https://api.example.de/users | jq .
# POST mit Response
curl -s -X POST \
-H "Content-Type: application/json" \
-d '{"name":"Test"}' \
https://api.example.de/users | jq .
# Status prüfen
if curl -s -o /dev/null -w "%{http_code}" https://example.de | grep -q "200"; then
echo "OK"
else
echo "Error"
fi
Health Check
#!/bin/bash
# health-check.sh
URL="https://example.de/health"
TIMEOUT=5
HTTP_CODE=$(curl -o /dev/null -s -w "%{http_code}" --max-time $TIMEOUT $URL)
if [ "$HTTP_CODE" = "200" ]; then
echo "OK"
exit 0
else
echo "FAIL: HTTP $HTTP_CODE"
exit 1
fi
Download mit Fortschritt
# curl
curl -# -O https://example.de/large.zip
# wget
wget --progress=bar:force https://example.de/large.zip
Parallel Downloads
# Mit xargs und curl
cat urls.txt | xargs -P 4 -I {} curl -O {}
# Oder aria2c
aria2c -i urls.txt -j 4
REST API CRUD
# Create
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"New Item"}' \
https://api.example.de/items
# Read
curl https://api.example.de/items/1
# Update
curl -X PUT -H "Content-Type: application/json" \
-d '{"name":"Updated Item"}' \
https://api.example.de/items/1
# Delete
curl -X DELETE https://api.example.de/items/1
Zusammenfassung
| curl Option | Funktion |
|---|
| -o | In Datei speichern |
| -O | Original-Name |
| -X | HTTP-Methode |
| -d | POST-Daten |
| -H | Header |
| -u | Basic Auth |
| -L | Redirects folgen |
| -v | Verbose |
| -s | Silent |
| -I | Nur Header |
| wget Option | Funktion |
|---|
| -O | Ausgabedatei |
| -c | Fortsetzen |
| -r | Rekursiv |
| --mirror | Website spiegeln |
| -q | Quiet |
| -b | Hintergrund |
Fazit
curl ist das universelle Tool für HTTP-Requests und API-Tests. wget eignet sich besser für Downloads und Website-Mirroring. Beide Tools sind unverzichtbar für Server-Administration. Mit jq lässt sich JSON-Output formatieren. Für automatisierte Tests sind curl-Scripts ideal.