Patch

You can patch a resource on an FHIR server by performing a Patch operation. This operation support allows you to modify a resource in place by supplying a delta. The Patch uses an HTTP PATCH against the URL [baseUrl]/[resourceType]/[id]. This operation requires a Content-Type header that specifies the MIME type of the payload.

For example, for a JSON patch payload the header value is: application/json-patch+json.

Updating a resource using patch operation: PATCH works by creating a patch document that specifies the changes that the client wishes to make to a resource, and submits that to the server. The server then applies those changes to the indicated resource and optionally returns the updated resource.

Each change in the patch document (and there can be multiple) specifies:

  • The operation that is to be performed (for example, add, remove, or replace).
  • The location in the resource to apply the operation.
  • The new value (if an add or change).

The following example shows how to perform a patch using a FHIR Patch.

Here is the initial patient resource.

{

"resourceType": "Patient",

"id" : "149954",

"birthDate": "1974-02-13"

}

Example 1: Add Operation

  • Adding the gender. If a target element is an object (for example, not an array) and already exists then the value is replaced. Otherwise, it is added.

    Table 11-1 Request

    HTTP Method PATCH

    URL

    http://<HOST>:<PORT>/oracle-fhir-server/fhir/Patient/149954

    Headers

    Content-Type: application/json-patch+json

    Request Body

    [{ "op": "add", "path": "/gender", "value": "male" }]

    Note:

    The Patch document is in an array as there can be any number of operations in a single PATCH call.

    Response: The FHIR endpoint will respond with a response similar to the following:

    {
    "resourceType": "Patient",
    "id": "149954",
    "meta": {
    "versionId": "2",
    "lastUpdated": "2022-04-24T19:05:13.111+05:30",
    "source": "#sieqifF8d5n5c2dU"
    },
    "text": {
    "status": "generated",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><table class=\"hapiPropertyTable\"><tbody/></table></div>"
    },
    "gender": "female"
    }
  • Adding an address. This element can repeat. It is represented in the resource instance as an array. It is possible to adjust an array, but unlike an object, the array will not be automatically created if it does not exist. So, the contents of the patch document will be different if there is no address already. It needs to be created first.

    Table 11-2 Request

    HTTP Method PATCH

    URL

    http://<HOST>:<PORT>/oracle-fhir-server/fhir/Patient/149954

    Headers

    Content-Type: application/json-patch+json

    Request Body

    [
    { "op": "add", "path": "/address", "value":[]},
    { "op": "add", "path": "/address/0", "value":
    {
    "use" : "home",
    "line" : ["<Sample Street Name>","avon"],
    "city" : "<City_Name>",
    "district": "<Sample Street Name>",
    "state": "Vic",
    "postalCode": "3999",
    "text":"<Sample Street Name>"
    }
    }
    ]

    Response: The FHIR endpoint will respond with a response similar to the following:

    {
    "resourceType": "Patient",
    "id": "149954",
    "meta": {
    "versionId": "3",
    "lastUpdated": "2022-04-24T19:17:02.229+05:30",
    "source": "#2dK6qlDOD2W4vQbh"
    },
    "text": {
    "status": "generated",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><table class=\"hapiPropertyTable\"><tbody/></table></div>"
    },
    "gender": "female",
    "address": [ {
    "use": "home",
    "text": "<Sample Street Name>",
    "line": [ "<Sample Street Name>", "avon" ],
    "city": "<City_Name>",
    "district": "<Sample Street Name>",
    "state": "Vic",
    "postalCode": "3999"
    } ]
    }

Example 2: Replace Operation

Update address content and birthDate object with replace operation.

Table 11-3 Request

HTTP Method PATCH

URL

http://<HOST>:<PORT>/oracle-fhir-server/fhir/Patient/149954

Headers

Content-Type: application/json-patch+json

Request Body

 [
{
"op": "replace",
"path": "/address/0/postalCode",
"value": "4000"
},
{
"op": "replace",
"path": "/birthDate",
"value": "1974-02-20"
}
]

Response: The FHIR endpoint will respond with a response similar to the following:

{
"resourceType": "Patient",
"id": "149954",
"meta": {
"versionId": "4",
"lastUpdated": "2022-04-24T19:37:51.559+05:30",
"source": "#fZYCuXUNmAoZYbDH"
},
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><table class=\"hapiPropertyTable\"><tbody/></table></div>"
},
"gender": "female",
"birthDate": "1974-02-20",
"address": [ {
"use": "home",
"text": "<Sample Street Name>t",
"line": [ "<Sample Street Name>", "avon" ],
"city": "<City_Name>",
"district": "<Sample Street Name>",
"state": "Vic",
"postalCode": "4000"
} ]
}

Example 3: Remove Operation

[
{"op": "remove", "path": "/address/0"}
]

For adjusting the array, you need to know the position of the element in the array to remove. If you want to be sure, then you can require that the client submit the actual address that they are removing so the server can check. This can be done using the test operation. The document below will remove the address only if its value matches.

[
{"op": "test", "path": "/address/0","value": { "use" : "home", "line" : ["<Sample Street Name>","avon"], "city" : "<City_Name>", "district": "<Sample Street Name>", "state": "Vic", "postalCode": "4000", "text":"<Sample Street Name>"}},
{"op": "remove", "path": "/address/0"}
]

Table 11-4 Request

HTTP Method PATCH

URL

http://<HOST>:<PORT>/oracle-fhir-server/fhir/Patient/149954

Headers

Content-Type: application/json-patch+json

Request Body

[
{"op": "test", "path": "/address/0","value": { "use" : "home",
"line" : ["<Sample Street Name>","avon"],
"city" : "<City_Name>",
"district": "<Sample Street Name>",
"state": "Vic",
"postalCode": "4000",
"text":"<Sample Street Name>"}},
{"op": "remove", "path": "/address/0"}
]

Response: The FHIR endpoint will respond with a response similar to the following:

{
"resourceType": "Patient",
"id": "149954",
"meta": {
"versionId": "5",
"lastUpdated": "2022-04-24T19:54:03.769+05:30",
"source": "#Bu5gN7pTIzZgLrM9"
},
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><table class=\"hapiPropertyTable\"><tbody/></table></div>"
},
"gender": "female",
"birthDate": "1974-02-20"
}