How to update a FHIR resource using a PATCH request

The PATCH request is an advanced FHIR feature that SDKs often remove the need for.

It’s similar to a PUT request, the main difference being it only updates specific elements or attributes of an existing resource.

My recent use case for PATCH was to change the status of various resources during a demo via a script. The script did not load any FHIR resources and it did not use an SDK. But it did need to change the status of individual resources throughout the demo.

There are different ways to run a PATCH request:

  • JSON Patch
  • XML Patch
  • FHIRPath Patch

I’ll be talking about FHIRPath Patch as I feel it’s the most FHIR native method.

I created a sample Encounter with a status of “planned” by POSTing it to the Firely test server here:

https://server.fire.ly/Encounter/a347e325-b33a-4db5-8ffd-f600c1f45f73?_format=json

Next, I moved the resource through a series of status changes using PATCH requests without ever knowing anything about the resource apart from its ID.

Data was passed in a Parameters resource that contained a parameter named “operation.”

Here’s a screenshot of the PATCH request I made to change the Encounter status from “planned” to “arrived”.

Key parameters in the resource are:

  • path
    The FHIRPath expression that identifies the element to be updated
  • type
    The action to carry out (add, replace, delete, etc.)
  • value
    The data itself

Updating primitive data types is straight forwards using PATCH. Where it gets difficult is when you want to update a more complicated multi-part element such as a CodeableConcept or an Identifier.

There’s a lot of trial and error in getting these right, and support does vary server to server. But if your use case involves updating FHIR resources without getting the full resource first, it’s well worth the effort.

Here’s a screenshot of the Vanya FHIR Viewer showing the 6 historic versions of the Encounter resource as it moves from “planned” to “finished” — all using PATCH requests.

More about FHIRPath Patch: http://hl7.org/fhir/fhirpatch.html
More about the different ways to run a PATCH: http://hl7.org/fhir/http.html#patch

Discussion

---

Work With Me

Discover more from Darren Devitt

Subscribe now to keep reading and get access to the full archive.

Continue reading