# cURL — Daten über die Kommandozeile übertragen

> Praxis-Guide zu cURL — HTTP-Requests, Header, Authentifizierung, Downloads und TLS auf der Kommandozeile, mit Beispielen für jeden Anwendungsfall.

Source: https://www.jpkc.com/db/cheatsheets/networking/curl/

<!-- PROSE:intro -->
cURL ist das Schweizer Taschenmesser für HTTP auf der Kommandozeile: Mit einem einzigen Befehl rufst du APIs ab, lädst Dateien herunter, testest Endpunkte oder debuggst Header. Das Tool spricht über zwanzig Protokolle – von HTTP und HTTPS über FTP bis SFTP – und ist auf praktisch jedem System vorinstalliert. Dieser Guide zeigt dir die wichtigsten Optionen für den Alltag, vom einfachen GET-Request bis zur Client-Zertifikats-Authentifizierung.
<!-- PROSE:intro:end -->

## Grundlegende Anfragen

`curl <url>` — Führt einen einfachen GET-Request aus und gibt die Antwort auf stdout aus.

```bash
curl https://api.example.com/users
```

`curl -s <url>` — Stiller Modus: unterdrückt Fortschrittsanzeige und Fehlermeldungen.

```bash
curl -s https://api.example.com/users
```

`curl -v <url>` — Ausführlicher Modus: zeigt Request- und Response-Header zum Debuggen.

```bash
curl -v https://api.example.com/users
```

`curl -I <url>` — Holt nur die HTTP-Antwort-Header (HEAD-Request).

```bash
curl -I https://www.example.com
```

`curl -L <url>` — Folgt Weiterleitungen automatisch.

```bash
curl -L https://example.com/redirect
```

## Header

`curl -H "<header>" <url>` — Sendet einen eigenen Request-Header.

```bash
curl -H "Accept: application/json" https://api.example.com/users
```

`curl -H "Content-Type: application/json" <url>` — Setzt den Content-Type-Header für den Request.

```bash
curl -H "Content-Type: application/json" https://api.example.com/users
```

`curl -H "Authorization: Bearer <token>" <url>` — Sendet ein Bearer-Token zur Authentifizierung.

```bash
curl -H "Authorization: Bearer eyJhbGci..." https://api.example.com/me
```

`curl -A "<user-agent>" <url>` — Setzt einen eigenen User-Agent-String.

```bash
curl -A "Mozilla/5.0" https://www.example.com
```

## Daten senden (POST & PUT)

`curl -X POST -d "<data>" <url>` — Sendet einen POST-Request mit URL-codierten Formulardaten.

```bash
curl -X POST -d "name=John&email=john@example.com" https://api.example.com/users
```

`curl -X POST -H "Content-Type: application/json" -d '<json>' <url>` — Sendet einen POST-Request mit JSON-Body.

```bash
curl -X POST -H "Content-Type: application/json" -d '{"name":"John"}' https://api.example.com/users
```

`curl -X PUT -d '<data>' <url>` — Sendet einen PUT-Request, um eine Ressource zu aktualisieren.

```bash
curl -X PUT -H "Content-Type: application/json" -d '{"name":"Jane"}' https://api.example.com/users/1
```

`curl -X PATCH -d '<data>' <url>` — Sendet einen PATCH-Request für Teil-Aktualisierungen.

```bash
curl -X PATCH -H "Content-Type: application/json" -d '{"email":"new@example.com"}' https://api.example.com/users/1
```

`curl -X DELETE <url>` — Sendet einen DELETE-Request, um eine Ressource zu entfernen.

```bash
curl -X DELETE https://api.example.com/users/1
```

`curl -F "file=@<path>" <url>` — Lädt eine Datei per Multipart-Formulardaten hoch.

```bash
curl -F "file=@photo.jpg" https://api.example.com/upload
```

`curl -d @<file> <url>` — Sendet Daten aus einer Datei als Request-Body.

```bash
curl -d @data.json -H "Content-Type: application/json" https://api.example.com/users
```

## Authentifizierung

`curl -u <user>:<password> <url>` — Sendet einen Request mit Basic-Authentifizierung.

```bash
curl -u admin:secret https://api.example.com/admin
```

`curl -u <user> <url>` — Fragt das Passwort interaktiv ab (sicherer).

```bash
curl -u admin https://api.example.com/admin
```

`curl --oauth2-bearer <token> <url>` — Sendet einen Request mit einem OAuth-2.0-Bearer-Token.

```bash
curl --oauth2-bearer eyJhbGci... https://api.example.com/me
```

`curl -b "<cookies>" <url>` — Sendet Cookies mit dem Request.

```bash
curl -b "session=abc123" https://www.example.com/dashboard
```

`curl -c <file> <url>` — Speichert Antwort-Cookies in einer Datei.

```bash
curl -c cookies.txt https://www.example.com/login
```

## Download & Ausgabe

`curl -o <file> <url>` — Speichert die Antwort in einer bestimmten Datei.

```bash
curl -o page.html https://www.example.com
```

`curl -O <url>` — Speichert die Antwort unter dem Remote-Dateinamen.

```bash
curl -O https://example.com/file.tar.gz
```

`curl -w "%{http_code}" -o /dev/null -s <url>` — Gibt nur den HTTP-Statuscode aus.

```bash
curl -w "%{http_code}" -o /dev/null -s https://www.example.com
```

`curl -w "%{time_total}" -o /dev/null -s <url>` — Misst die gesamte Antwortzeit.

```bash
curl -w "%{time_total}" -o /dev/null -s https://www.example.com
```

`curl -C - -O <url>` — Setzt einen abgebrochenen Download fort.

```bash
curl -C - -O https://example.com/largefile.zip
```

## SSL/TLS

`curl -k <url>` — Überspringt die Prüfung des SSL-Zertifikats (unsicher, nur zum Testen).

```bash
curl -k https://self-signed.example.com
```

`curl --cacert <file> <url>` — Nutzt ein eigenes CA-Zertifikat zur Prüfung.

```bash
curl --cacert ca-bundle.crt https://api.example.com
```

`curl --cert <cert> --key <key> <url>` — Nutzt Client-Zertifikats-Authentifizierung.

```bash
curl --cert client.crt --key client.key https://api.example.com
```

## Erweiterte Optionen

`curl -x <proxy> <url>` — Nutzt einen Proxy-Server für den Request.

```bash
curl -x http://proxy.example.com:8080 https://www.example.com
```

`curl --connect-timeout <seconds> <url>` — Setzt die maximale Zeit für den Verbindungsaufbau.

```bash
curl --connect-timeout 5 https://api.example.com
```

`curl -m <seconds> <url>` — Setzt die maximale Zeit für den gesamten Vorgang.

```bash
curl -m 30 https://api.example.com/slow-endpoint
```

`curl --retry <num> <url>` — Wiederholt den Request bei vorübergehenden Fehlern.

```bash
curl --retry 3 https://api.example.com/flaky
```

`curl -s <url> | jq .` — Leitet die JSON-Antwort zur formatierten Ausgabe an jq weiter.

```bash
curl -s https://api.example.com/users | jq .
```

<!-- PROSE:outro -->
## Fazit

cURL gehört in das Repertoire jeder Entwicklerin und jedes Administrators: Was als schneller API-Test beginnt, ersetzt rasch ganze Skripte. Wenn du regelmäßig komplexe Requests baust, lohnt sich der Blick auf `--config`-Dateien und das `-w`-Ausgabeformat für maschinenlesbare Resultate – so wird aus dem Einzeiler ein wiederverwendbares Werkzeug.

## Weiterführende Links

- [curl – offizielle Dokumentation](https://curl.se/docs/) – Referenz und Handbuch (englisch)
- [curl(1) – Manpage](https://curl.se/docs/manpage.html) – alle Optionen im Überblick (englisch)
- [cURL – Wikipedia](https://de.wikipedia.org/wiki/CURL) – Hintergrund und Geschichte
<!-- PROSE:outro:end -->

## Verwandte Kommandos

- [httpie](https://www.jpkc.com/db/cheatsheets/networking/httpie/) – moderner, benutzerfreundlicher HTTP-Client
- [wget](https://www.jpkc.com/db/cheatsheets/networking/wget/) – Dateien und ganze Sites herunterladen
- [dig](https://www.jpkc.com/db/cheatsheets/networking/dig/) – DNS-Auflösung prüfen, bevor du Requests sendest

