Müssen Sie Ihre API testen, indem Sie JSON-Daten aus einer Datei senden? Hier sind die Curl-Befehle.
JSON-Datei an Localhost posten
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-d @data.json
Ersetzen:
8080mit Ihrem Port/api/endpointmit Ihrem API-Pfaddata.jsonmit Ihrem JSON-Dateipfad
Die @ weist Curl an, aus der Datei zu lesen.
Das Symbol
PUT JSON-Datei
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
curl -X POST http://localhost:8080/api/endpoint -H "Content-Type: application/json" -d @data.json
Alles auf einer Zeile.
Antwortkopfzeilen anzeigen
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)
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:
curl -X POST http://localhost:8080/api/endpoint \
-H "Content-Type: application/json" \
-u username:password \
-d @data.json
Inhabertoken:
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:
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)
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
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
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://vshttps://
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
-vFlag, um die vollständige Anfrage anzuzeigen
Beispiel-JSON-Datei
Erstellen data.json:
{
"name": "Test User",
"email": "test@example.com",
"active": true,
"roles": ["admin", "user"]
}
Dann führen Sie Folgendes aus:
curl -X POST http://localhost:8080/users -H "Content-Type: application/json" -d @data.json
Fazit
Grundlegender POST-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.