Skip to content

Latest commit

 

History

History
184 lines (135 loc) · 4.31 KB

patch.md

File metadata and controls

184 lines (135 loc) · 4.31 KB
description
Updating a part of your resource

Patch

All examples can be run in Postman. Here's a web view of these examples.

Run in Postman

In most Operations in FHIR, you manipulate a resource as a whole (create, update, delete operations). But sometimes you want to update specific data elements in a resource and do not care about the rest. In other words, you need an element/attribute level operation.

With the patch operation, you can update a part of a resource by sending a declarative description of operations that should be performed on an existing resource. To describe these operations in Aidbox, you can use different notations (methods):

Patch Method

You can specify a patch method by the content-type header or by the _method parameter.

method parameter header
json-patch json-patch application/json-patch+json
merge-patch merge-patch application/merge-patch+json

If the method is not specified, Aidbox will try to guess it by the following algorithm:

  • if the payload is an array — json-patch
  • else merge-patch

Operation Description

{% swagger baseUrl="[base-url]/:resourceType/:id" path="" method="patch" summary="Patch Operation" %} {% swagger-description %}

{% endswagger-description %}

{% swagger-parameter in="path" name="_method" type="string" %} Can be

json-patch

,

merge-patch

(and

fhir-patch

in the future) {% endswagger-parameter %}

{% swagger-parameter in="header" name="content-type" type="string" %} See the

content-type

header in the table above {% endswagger-parameter %}

{% swagger-parameter in="body" name="" type="string" %} JSON or YAML representation of transformation rules in accordance with

_method {% endswagger-parameter %}

{% swagger-response status="200" description="" %}

Updated resource

{% endswagger-response %} {% endswagger %}

Example

{% hint style="info" %} You can exercise this tutorial using REST Console — just copy/paste queries into your console! {% endhint %}

Let's suppose we've created a Patient resource with the id pt-1

POST /Patient

resourceType: Patient
id: pt-1
active: true
name:
  - given: ['John']
    family: Doe
    use: official
  - given: ['Johny']
    family: Doe
telecom:
  -  system: phone
     value: '(03) 5555 6473'
     use: work
     rank: 1
birthDate: '1979-01-01'

# 200

id: pt-1
resourceType: Patient
name:
- use: official
  given:
  - John
  family: Doe
- given:
  - Johny
  family: Doe
active: true
telecom:
- use: work
  rank: 1
  value: "(03) 5555 6473"
  system: phone
birthDate: '1979-01-01'

{% hint style="info" %} You can copy/paste this request into REST Console of Aidbox.Cloud. {% endhint %}

Merge Patch

Let's say we want to switch an active flag to false and remove telecom:

PATCH /Patient/pt-1?_method=merge-patch

active: false
telecom: null

# 200

id: pt-1
resourceType: Patient
name:
- use: official
  given:
  - John
  family: Doe
- given:
  - Johny
  family: Doe
active: false
birthDate: '1979-01-01'

JSON Patch

With JSON patch, we can do more sophisticated transformations — change the first given name, delete the second name, and change the active attribute value to true:

PATCH /Patient/pt-1

- op: replace
  path: '/name/0/given/0'
  value: Nikolai
- op: remove
  path: '/name/1'
- op: replace
  path: '/active'
  value: true
  
# 200

# response 200

id: pt-1
resourceType: Patient
name:
- use: official
  given:
  - Nikolai
  family: Doe
active: true
birthDate: '1979-01-01'