Please enable JavaScript to view this site.

PRD_SIS_004_Developer's Guide 2.6

Navigation: Implementation details

Using JSON-PATCH to update a resource

Scroll Prev Top Next More

To update a resource state, the services are exposing the PATCH verb on the resources API:

PATCH https://{domain}/{platform}/enduser/orders/v1/orders/{OrderID}

 

The PATCH API expects:

Use of the Optimistic Concurrency Control mechanism on the eTag field

Use of the application/json-patch+json content type

Use of a payload conform to RFC 6902

An input computed (or built) as a diff on the resource payload as provided by the GET API, compacted by the service's own context.

oIn other terms, a JSON diff is computed on the JSON representation provided by the service.

 

The way to do it using JSON-LD is the following:

1.GET the resource to PATCH

Keep this original representation in memory

2.Compact it with your client context

3.Manipulate your internal representation to apply your changes

4.Compact it back to the original service context (given by the GET API)

5.Compute the JSON diff between

the representation retrieved in 1,

and the representation with changes compacted in 4.

6.Send the JSON diff to the PATCH API as a JSON, without context

 

Example:

1.Sample GET request to get the original resource

const {orderFromService, serviceContext, eTag} = await fetchResource({url});

->

GET https://{domain}/{platform}/enduser/orders/v1/orders/123

-->

eTag: "3263317"

{

 "@context": "...",

 "orderStatus": "sc:OrderProcessing"

 ...

}

2.Compact with your context

const order = await jsonld.compact(orderFromService, clientContext);

3.Manipulate your internal representation

order.orderStatus = ‘sp:OrderInTransit’;

4.Compact it back to the original service context

order = await jsonld.compact(orderFromService, serviceContext);

5.Compute the JSON diff between:

diff = await jsonpatch.compare(orderFromService, order);

6.Sample PATCH request to update its status:

const {responseCode, responseBody} = await patchResource({url, diff, eTag});

 

PATCH https://{domain}/{platform}/enduser/orders/v1/orders/123

content-type: application/json-patch+json

if-match: "3263317"

[

 {"op": "replace", "path":"/orderStatus", "value": "sp:OrderInTransit"}

]

-->

204 No Content

eTag: "247665187"

location: /development/enduser/orders/v1/orders/123