FHIR API
Create, read, update, delete
Every basic interaction with working commands for this server, including PATCH and how HAPI handles deletes.
All examples assume FHIR=http://192.168.1.181:8080/fhir. See Quickstart.
Create#
POST lets the server assign the id:
curl -s -X POST "$FHIR/Patient" -H "Content-Type: application/fhir+json" \
-H "Prefer: return=representation" \
-d '{"resourceType":"Patient","name":[{"family":"Nguyen","given":["Linh"]}],"gender":"female","birthDate":"1984-07-12"}'The response is 201 Created with a Location header pointing at the new version.
Create with your own id#
PUT to an id that doesn't exist yet creates it. HAPI's default client_id_strategy is ALPHANUMERIC, which means ids you choose must contain at least one letter. Purely numeric ids are reserved for the server.
curl -s -X PUT "$FHIR/Patient/drill-linh" -H "Content-Type: application/fhir+json" \
-d '{"resourceType":"Patient","id":"drill-linh","name":[{"family":"Nguyen","given":["Linh"]}]}'The id in the body must match the id in the URL, or HAPI rejects the request.
Read#
curl -s "$FHIR/Patient/drill-linh"
curl -s "$FHIR/Patient/drill-linh/_history/1" # a specific version (vread)
curl -s "$FHIR/Patient/drill-linh?_elements=name,gender"
curl -s "$FHIR/Patient/drill-linh?_format=xml" # XML instead of JSONUpdate#
PUT replaces the whole resource. Any field you leave out is removed.
curl -s -X PUT "$FHIR/Patient/drill-linh" -H "Content-Type: application/fhir+json" \
-d '{"resourceType":"Patient","id":"drill-linh","name":[{"family":"Nguyen","given":["Linh","Thi"]}],"gender":"female"}'PUT is not a merge
The most common integration bug is reading part of a resource, changing one field and PUTting it back, which deletes everything you didn't read. Always GET the full resource, change it, then PUT it back, or use PATCH.
Patch#
PATCH changes only part of a resource. HAPI accepts two formats.
JSON Patch#
curl -s -X PATCH "$FHIR/Patient/drill-linh" -H "Content-Type: application/json-patch+json" \
-d '[{"op":"replace","path":"/gender","value":"female"},
{"op":"add","path":"/telecom","value":[{"system":"phone","value":"0400 000 000","use":"mobile"}]}]'FHIRPath Patch#
FHIRPath Patch is FHIR-native and safer on arrays because it addresses elements by meaning rather than by position:
curl -s -X PATCH "$FHIR/Patient/drill-linh" -H "Content-Type: application/fhir+json" -d '{
"resourceType": "Parameters",
"parameter": [{
"name": "operation",
"part": [
{"name": "type", "valueCode": "replace"},
{"name": "path", "valueString": "Patient.birthDate"},
{"name": "value", "valueDate": "1984-07-13"}
]
}]
}'Delete#
curl -s -X DELETE "$FHIR/Patient/drill-linh" -i | head -1- Deletes are soft. HAPI keeps the history and records a deleted version, and a read of the resource returns
410 Gone. - References block deletes. If other resources point at the one you're deleting, HAPI refuses with
409 Conflict. Delete those resources first, or use cascading delete (below). - To remove data for good, use
$expunge.
Cascading delete#
HAPI can delete a resource and everything that references it in one call, but only when the server allows it. It's off in this lab, so ?_cascade=delete currently returns 409. To turn it on, add this to the hapi service's environment in ~/fhir-lab/hapi/compose.yml and run docker compose up -d:
hapi.fhir.allow_cascading_deletes: "true"Then:
curl -s -X DELETE "$FHIR/Patient/drill-linh?_cascade=delete"Delete many#
allow_multiple_delete is switched on in this lab, so a conditional delete can remove every match:
curl -s -X DELETE "$FHIR/Patient?identifier=https://fhirdrill.dev/mrn|"Check the search first
Run the same URL as a GET search before you DELETE it. A typo in a search parameter can make HAPI ignore it, so the delete matches everything.
Status codes at a glance#
| Code | Meaning in practice |
|---|---|
200 OK |
Read, update or search succeeded |
201 Created |
Create succeeded; see the Location header |
204 No Content |
Delete succeeded, or Prefer: return=minimal was set |
400 Bad Request |
The body couldn't be parsed; read the OperationOutcome |
404 Not Found |
The resource never existed |
409 Conflict |
Delete blocked by references, or a version conflict |
410 Gone |
The resource was deleted |
412 Precondition Failed |
If-Match didn't match the current version, or a conditional update matched more than one resource |
422 Unprocessable Entity |
The resource parsed but failed validation |