HTTP PATCH Method

Die HTTP-Methode PATCH führt partielle Änderungen an einer Ressource durch. Im Gegensatz zu PUT, das die vollständige Ressource ersetzt, modifiziert PATCH nur die angegebenen Felder und wird für effiziente Updates in REST-APIs verwendet.

Typ

HTTP-Methode

Syntax

PATCH-Request mit Ressourcen-URI und Patch-Dokument im Body.

http
PATCH /api/users/42 HTTP/1.1
Host: api.example.com
Content-Type: application/merge-patch+json

Direktiven

Die Direktiven definieren Patch-Semantik und Payload-Formate.

Request-Body
Enthält Patch-Dokument mit Änderungen, Format via Content-Type definiert.
application/merge-patch+json
JSON Merge Patch Format, Simple Object Merge-Semantik.
application/json-patch+json
JSON Patch Format mit Operationen wie add, remove, replace, move.
200 OK
Erfolgreicher Patch mit geänderter Ressource im Response-Body.
204 No Content
Erfolgreicher Patch ohne Response-Body.
409 Conflict
Patch kann nicht angewendet werden, z.B. bei Concurrent Modification.
Not Idempotent
Wiederholte PATCH-Requests können unterschiedliche Ergebnisse liefern.

Beispiele

Nachfolgend finden Sie praktische Anwendungsbeispiele für die PATCH-Methode.

Beispiel 1 JSON Merge Patch

http
PATCH /api/users/alice HTTP/1.1
Host: api.example.com
Content-Type: application/merge-patch+json
Authorization: Bearer token_abc

{
  "email": "alice.new@example.com",
  "phone": "+49 123 456789"
}

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": "alice",
  "name": "Alice Schmidt",
  "email": "alice.new@example.com",
  "phone": "+49 123 456789"
}

Merge-Patch aktualisiert nur Email und Phone, andere Felder bleiben unverändert.

Beispiel 2 JSON Patch Operations

http
PATCH /api/articles/123 HTTP/1.1
Host: blog.example.com
Content-Type: application/json-patch+json

[
  {"op": "replace", "path": "/title", "value": "New Title"},
  {"op": "add", "path": "/tags/-", "value": "tutorial"},
  {"op": "remove", "path": "/draft"}
]

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 123,
  "title": "New Title",
  "tags": ["tech", "api", "tutorial"]
}

JSON-Patch erlaubt komplexe Operationen: Replace title, Add tag, Remove draft-Flag.

Beispiel 3 Optimistic Locking

http
PATCH /api/documents/doc-42 HTTP/1.1
Host: api.example.com
If-Match: "etag-abc123"
Content-Type: application/merge-patch+json

{"status": "published"}

HTTP/1.1 412 Precondition Failed
ETag: "etag-xyz789"

{
  "error": "Document was modified by another user",
  "current_etag": "etag-xyz789"
}

Concurrent Modification Detection via ETag, Server lehnt veralteten Patch ab.

PATCH Partial Update Flow

PATCH Partial-Update-Ablauf

Vorteile für die Systemarchitektur

  • Bandwidth-Effizienz: Nur geänderte Felder übertragen statt vollständiger Ressource
  • Granulare Updates: JSON-Patch erlaubt Array-Manipulationen und komplexe Transformationen
  • Concurrent Modification Detection: If-Match/ETag Pattern verhindert Lost-Update-Problem

Spezifikation

RFC 5789 – PATCH Method for HTTP https://www.rfc-editor.org/rfc/rfc5789.html RFC 7396 – JSON Merge Patch https://www.rfc-editor.org/rfc/rfc7396.html

Weitere Spezifikationen

PUT Method, Content-Type Header, HTTP Status 200 - OK