Zum Inhalt

curl - HTTP Client und Datentransfer

_

Umfassende Curl-Befehle für HTTP-Anfragen, API-Tests und Datenübertragung auf allen Plattformen.

Basic HTTP Anfragen

GET Requests

Command Description
INLINE_CODE_18 Basic GET request
INLINE_CODE_19 Verbose output with headers
INLINE_CODE_20 Include response headers
INLINE_CODE_21 HEAD request only
INLINE_CODE_22 Follow redirects

POST Requests

Command Description
INLINE_CODE_23 Basic POST request
INLINE_CODE_24 POST with data
INLINE_CODE_25 POST data from file
INLINE_CODE_26 File upload

Andere HTTP Methods

Command Description
INLINE_CODE_27 PUT request
INLINE_CODE_28 DELETE request
INLINE_CODE_29 PATCH request

Header und Authentication

Benutzerdefinierte Headers

Command Description
INLINE_CODE_30 Set content type
INLINE_CODE_31 Bearer token auth
INLINE_CODE_32 Custom user agent
INLINE_CODE_33 Accept header
_
### Authentication Methods
Command Description
--------- -------------
INLINE_CODE_34 Basic authentication
INLINE_CODE_35 Prompt for password
INLINE_CODE_36 OAuth2 bearer token
INLINE_CODE_37 Digest authentication

API Schlüsselauthentifizierung

```bash

API key in header

curl -H "X-API-Key: your-api-key" https://api.example.com

API key in query parameter

curl "https://api.example.com?api_key=your-api-key"

Multiple headers

curl -H "Authorization: Bearer token" \ -H "Content-Type: application/json" \ https://api.example.com ```_

oder Datenformate

JSON Data

```bash

POST JSON data

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

JSON from file

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

Form Data

```bash

URL-encoded form data

curl -d "name=John&age=30" https://api.example.com/users

Multipart form data

curl -F "name=John" -F "age=30" https://api.example.com/users

File upload with form data

curl -F "file=@document.pdf" \ -F "description=Important document" \ https://api.example.com/upload ```_

XML Data

```bash

POST XML data

curl -X POST \ -H "Content-Type: application/xml" \ -d 'John30' \ https://api.example.com/users ```_

Dateioperationen

Dateien herunterladen

Command Description
INLINE_CODE_38 Download with original name
INLINE_CODE_39 Download with custom name
INLINE_CODE_40 Resume interrupted download
INLINE_CODE_41 Limit download speed

Dateien hochladen

Command Description
INLINE_CODE_42 Upload via FTP
INLINE_CODE_43 HTTP file upload
INLINE_CODE_44 PUT file upload

Mehrere Dateien

```bash

Download multiple files

curl -O https://example.com/file1.zip -O https://example.com/file2.zip

Upload multiple files

curl -F "file1=@doc1.pdf" -F "file2=@doc2.pdf" https://api.example.com/upload ```_

Erweiterte Optionen

Timeouts and Retries

Command Description
INLINE_CODE_45 Connection timeout (seconds)
INLINE_CODE_46 Maximum total time
INLINE_CODE_47 Retry on failure
INLINE_CODE_48 Delay between retries

SSL/TLS Optionen

Command Description
INLINE_CODE_49 Ignore SSL certificate errors
INLINE_CODE_50 Use custom CA certificate
INLINE_CODE_51 Use client certificate
INLINE_CODE_52 Force TLS version

Proxy and Network

Command Description
INLINE_CODE_53 Use HTTP proxy
INLINE_CODE_54 Use SOCKS5 proxy
INLINE_CODE_55 Use specific network interface
INLINE_CODE_56 Use custom DNS servers

Ausgabe und Formatierung

Output Control

Command Description
INLINE_CODE_57 Silent mode (no progress)
INLINE_CODE_58 Show errors even in silent mode
INLINE_CODE_59 Discard output
INLINE_CODE_60 Show only HTTP status code
_
### Response Information
```bash
# Get detailed timing information
curl -w "@curl-format.txt" https://example.com

curl-format.txt content:

time_namelookup: %\\{time_namelookup\\}\n

time_connect: %\\{time_connect\\}\n

time_appconnect: %\\{time_appconnect\\}\n

time_pretransfer: %\\{time_pretransfer\\}\n

time_redirect: %\\{time_redirect\\}\n

time_starttransfer: %\\{time_starttransfer\\}\n

----------\n

time_total: %\\{time_total\\}\n

```_

JSON Processing

```bash

Pretty print JSON with jq

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

Extract specific fields

curl -s https://api.example.com/users|jq '.[]|.name'

Filter results

curl -s https://api.example.com/users|jq '.[]|select(.age > 25)' ```_

Testen und Debuggen

API Testing

```bash

Test REST API endpoints

curl -X GET https://api.example.com/users curl -X POST -d '\\{"name":"John"\\}' https://api.example.com/users curl -X PUT -d '\\{"name":"Jane"\\}' https://api.example.com/users/1 curl -X DELETE https://api.example.com/users/1

Test with different content types

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

Performance Testing

```bash

Measure response time

curl -w "Total time: %\\{time_total\\}s\n" -o /dev/null -s https://example.com

Test multiple requests

for i in \\{1..10\\}; do curl -w "%\\{time_total\\}\n" -o /dev/null -s https://example.com done ```_

Error Handling

```bash

Check HTTP status codes

http_code=$(curl -s -o /dev/null -w "%\\{http_code\\}" https://example.com) if [ $http_code -eq 200 ]; then echo "Success" else echo "Error: HTTP $http_code" fi ```_

Konfiguration und Skripte

Konfigurationsdatei

```bash

~/.curlrc configuration file

user-agent = "MyApp/1.0" connect-timeout = 10 max-time = 30 show-error silent ```_

Bash Scripting

```bash

!/bin/bash

API testing script

BASE_URL="https://api.example.com" API_KEY="your-api-key"

Function to make authenticated requests

api_request() \\{ local method=\(1 local endpoint=\)2 local data=$3

curl -X "$method" \
     -H "Authorization: Bearer $API_KEY" \
     -H "Content-Type: application/json" \
     $\\\\{data:+-d "$data"\\\\} \
     "$BASE_URL$endpoint"

\\}

Usage examples

api_request GET "/users" api_request POST "/users" '\\{"name":"John","email":"john@example.com"\\}' ```_

Die besten Praktiken der Sicherheit

Secure Authentication

```bash

Use environment variables for sensitive data

export API_TOKEN="your-secret-token" curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com

Read credentials from file

curl -K credentials.txt https://api.example.com

credentials.txt:

header = "Authorization: Bearer your-token"

```_

Zertifikat Verifikation

```bash

Always verify SSL certificates in production

curl --cacert /path/to/ca-bundle.crt https://api.example.com

For development only (not recommended for production)

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

Häufige Anwendungsfälle

Web Scraping

```bash

Download webpage

curl -L https://example.com > page.html

Follow redirects and save cookies

curl -L -c cookies.txt -b cookies.txt https://example.com

Set user agent to avoid blocking

curl -H "User-Agent: Mozilla/5.0 (compatible; bot)" https://example.com ```_

API Integration

```bash

GitHub API example

curl -H "Authorization: token your-github-token" \ https://api.github.com/user/repos

Weather API example

curl "https://api.openweathermap.org/data/2.5/weather?q=London&appid=your-api-key"

Slack webhook example

curl -X POST \ -H "Content-Type: application/json" \ -d '\\{"text":"Hello from curl!"\\}' \ https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK ```_

Dateiübertragung

```bash

Upload to cloud storage

curl -X PUT \ -H "Authorization: Bearer token" \ --upload-file document.pdf \ https://api.storage.com/files/document.pdf

Download with progress bar

curl --progress-bar -O https://example.com/largefile.zip ```_

Fehlerbehebung

Gemeinsame Themen

Problem Solution
SSL certificate errors Use INLINE_CODE_61 for testing, fix certificates for production
Connection timeout Increase INLINE_CODE_62 value
Slow downloads Use INLINE_CODE_63 to control bandwidth
Authentication failures Check credentials and authentication method
_
### Debugging Commands
```bash
# Verbose output for debugging
curl -v https://example.com

Trace all network activity

curl --trace trace.txt https://example.com

Show only headers

curl -I https://example.com

Test connectivity

curl -I --connect-timeout 5 https://example.com ```_