Updating existing resources (PATCH)

Last updated on
17 December 2022

This page shows examples of PATCH requests for the JSON:API module.

Enabling update operation

Visit /admin/config/services/jsonapi
and check the "Accept all JSON:API create, read, update, and delete operations." option.

jsonapi-accept-create-read-update-delete

Authentication

Typically some form of authentication is used for PATCH requests. The examples below all use Basic Authentication. Enable the HTTP Basic Authentication module, set the permission for the API user (and role) and add set the encoded username and password to the 'Authorization' request header.

The example header on this page requires a Drupal user 'api' with password 'api'. This user must have permission to update the requested content.

Headers

The follow headers are required on all PATCH request to get a proper JSON:API request and response.

  • Accept: application/vnd.api+json
  • Content-Type:application/vnd.api+json

The following header is needed for the examples to work:

  • Authorization:Basic VALUE

You can obtain the VALUE above in your applications by using command line:

echo -n "USERNAME:PASSWORD" | base64 -w0

Use the returned value above after BASIC. In this example, the credentials of api:api returns YXBpOmFwaQ==, so the header will look like:

Authorization:Basic YXBpOmFwaQ==

Basic PATCH request

URL: http://example.com/jsonapi/node/article/{{article_uuid}}

Request body

{
  "data": {
    "type": "node--article",
    "id": "{{article_uuid}}",
    "attributes": {
      "title": "My updated title"
    }
  }
}

The "id" is required. Add the attributes that should be updated.

Response

HTTP 200 response. The response body with the JsonApi response of the updated entity.

PATCH with more attributes

URL: http://example.com/jsonapi/node/article/{{article_uuid}}

Request body

{
  "data": {
    "type": "node--article",
    "id": "{{article_uuid}}",
    "attributes": {
      "title": "My updated title",
      "body": {
        "value": "Updated body text",
        "format": "plain_text",
        "summary": "Updated summary"
      }
    },
    "relationships": {
      "uid": {
        "data": {
          "type": "user--user",
          "id": "{{user_uuid}}"
        }
      }
    }
  }
}

Response

HTTP 200 response. The response body with updated body, summary and author updated entity.

PATCH to remove a relationship

{
  "data": {
    "type": "node--article",
    "id": "{{article_uuid}}",
    "attributes": {
      "title": "My updated title",
      "body": {
        "value": "Updated body text",
        "format": "plain_text",
        "summary": "Updated summary"
      }
    },
    "relationships": {
      "my_entity_reference_field": {
        "data": {},
      }
    }
  }
}

Response

HTTP 200 response. The response body with the updated body, summary, and entity reference removed.

Help improve this page

Page status: No known problems

You can: