Müssen Sie Ihre API testen, indem Sie JSON-Daten aus einer Datei senden? Hier sind die Curl-Befehle.

JSON-Datei an Localhost posten

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -d @data.json

Ersetzen:

  • 8080 mit Ihrem Port
  • /api/endpoint mit Ihrem API-Pfad
  • data.json mit Ihrem JSON-Dateipfad

Die @ weist Curl an, aus der Datei zu lesen. Das Symbol

PUT JSON-Datei

Befehl
curl -X PUT http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -d @data.json

Wie POST, nur mit der PUT-Methode.

Einzeiler-Version

Befehl
curl -X POST http://localhost:8080/api/endpoint -H "Content-Type: application/json" -d @data.json

Alles auf einer Zeile.

Antwortkopfzeilen anzeigen

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -d @data.json \
  -i

-i schließt Antwortheader in die Ausgabe ein.

Ausführliche Ausgabe (Debug)

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -d @data.json \
  -v

-v zeigt die vollständigen Anfrage- und Antwortdetails.

Mit Authentifizierung

Basisauthentifizierung:

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -u username:password \
  -d @data.json

Inhabertoken:

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-token-here" \
  -d @data.json

API-Schlüssel:

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your-api-key" \
  -d @data.json

Inline-JSON senden (keine Datei)

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -d '{"key":"value","number":123}'

Einfache Anführungszeichen verhindern die Shell-Interpretation.

Datei in anderem Verzeichnis

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -d @/path/to/data.json

Verwenden Sie einen absoluten oder relativen Pfad.

Antwort in Datei speichern

Befehl
curl -X POST http://localhost:8080/api/endpoint \
  -H "Content-Type: application/json" \
  -d @data.json \
  -o response.json

Antwort gespeichert unter response.json.

Gemeinsame Localhost-Ports

  • 3000: Node.js, React-Entwicklungsserver
  • 8000: Python-HTTP-Server, Django
  • 8080: Common Java, allgemeine Entwicklung
  • 5000: Flaschenstandard
  • 4200: Angular-Entwicklungsserver
  • 3001: Alternative Node.js

Fehlerbehebung

Fehler „Verbindung fehlgeschlagen“:

  • API läuft nicht
  • Falsche Portnummer
  • Überprüfen http:// vs https://

Fehler „curl: no such file“:

  • JSON-Datei existiert nicht
  • Falscher Dateipfad
  • Überprüfen Sie das aktuelle Verzeichnis mit pwd

Unerwartete Antwort:

  • Überprüfen Sie die JSON-Syntax mit cat data.json
  • Validieren Sie JSON auf jsonlint.com
  • Verwendung -v Flag, um die vollständige Anfrage anzuzeigen

Beispiel-JSON-Datei

Erstellen data.json:

Befehl
{
  "name": "Test User",
  "email": "test@example.com",
  "active": true,
  "roles": ["admin", "user"]
}

Dann führen Sie Folgendes aus:

Befehl
curl -X POST http://localhost:8080/users -H "Content-Type: application/json" -d @data.json

Fazit

Grundlegender POST-Befehl:

Befehl
curl -X POST http://localhost:8080/api/endpoint -H "Content-Type: application/json" -d @data.json

Die @ Symbol liest aus Datei. Ändern Sie POST für PUT-Anfragen in PUT. Hinzufügen Das Symbol -v zum Debuggen. Hinzufügen -i für Antwortheader.

Funktioniert während der Entwicklung mit jeder Localhost-API.