Drupal 8.x Update existing content or data with rest API

Reading time: 3 minutes

Problem

How to Update or PATCH existing Article or any other custom content type with image in Drupal 8.x site via rest API by using JSON:API and REST module.

Final output

At the end we will be able to Update an Article node content with image field using rest API by JSON:API in Drupal 8.x site.

Prerequisite

Before updating or PATCH Article content type with image, we have to upload the image file correctly and then we link that image with article update HTTP method via rest API.

If you want to make sure, How to Post or upload files by JSON rest API in Drupal 8.x site check it out

The process to updating or PATCH a content with image via rest API in Drupal 8.x site is, to upload the image file first and then in uploaded file response we will get fid value.

So, if you already did that, then just keep the fid value with you and we will use it in a while.

If you are ready until there, then let’s jump to achieve our goal to Update article content via rest API in Drupal 8 site.

NOTE: Also make sure all the configurations for JSON:API and REST modules has been setup correctly by visiting Prerequisite article.

Solution

1. Open up the API testing tool, We are going to test it through rest API using Postman.

Update Article content type API

* Request:
Method:
PATCH


URL:
{{BASE_URL}}/node/[nid]?_format=hal_json
* nid:
This is the node Id of the content you want to update.

Headers:
* Authorization: Basic <credential> Β 
Basic authentication is where the username and password-based authentication takes place.
* X-CSRF-Token: We can get CSRF token in response of Login API.
To see how to get CSRF token in Drupal site
* Content-Type: application/hal+json


Body:

{
    "_links": {
        "type": {
            "href": "[BASE_URL]/rest/type/node/article"
        }
    },
    "title":{
        "value":"Updated Article"
    },
    "field_image": [
        {
            "target_id": 4
        }
    ]
}

NOTE:
* Put your fid value which you can get in response of uploaded file rest API in target_id, or if you uploaded file manually then put nid of file.
* Image is optional and you can update record without image field by just remove field_image from API body request.

To get the machine name for image field in Article content type in Drupal 8.x site.

Go to Structure -> Content types -> Article -> Manage fieldsΒ  and there you can see all the machine names for fields e-g field_image for article image.

Get machine name for image field in content type article Drupal 8 site
Get machine name for image field in Article content type

* Response

{
    "_links": {
        "self": {
            "href": "https://dev-solution-spirit-cms.pantheonsite.io/node/4?_format=hal_json"
        },
        "type": {
            "href": "https://dev-solution-spirit-cms.pantheonsite.io/rest/type/node/article"
        },
        "https://dev-solution-spirit-cms.pantheonsite.io/rest/relation/node/article/revision_uid": [
            {
                "href": "https://dev-solution-spirit-cms.pantheonsite.io/user/1?_format=hal_json"
            }
        ],
        "https://dev-solution-spirit-cms.pantheonsite.io/rest/relation/node/article/uid": [
            {
                "href": "https://dev-solution-spirit-cms.pantheonsite.io/user/1?_format=hal_json",
                "lang": "en"
            }
        ],
        "https://dev-solution-spirit-cms.pantheonsite.io/rest/relation/node/article/field_image": [
            {
                "href": "https://dev-solution-spirit-cms.pantheonsite.io/file/4?_format=hal_json",
                "lang": "en"
            }
        ]
    },
    "nid": [
        {
            "value": 4
        }
    ],
    "uuid": [
        {
            "value": "2d9e718e-2b6f-4e9e-9afd-3469fab491de"
        }
    ],
    "vid": [
        {
            "value": 4
        }
    ],
    "langcode": [
        {
            "value": "en",
            "lang": "en"
        }
    ],
    "type": [
        {
            "target_id": "article"
        }
    ],
    "revision_timestamp": [
        {
            "value": "2020-06-21T14:30:17+00:00",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "_embedded": {
        "https://dev-solution-spirit-cms.pantheonsite.io/rest/relation/node/article/revision_uid": [
            {
                "_links": {
                    "self": {
                        "href": "https://dev-solution-spirit-cms.pantheonsite.io/user/1?_format=hal_json"
                    },
                    "type": {
                        "href": "https://dev-solution-spirit-cms.pantheonsite.io/rest/type/user/user"
                    }
                },
                "uuid": [
                    {
                        "value": "f2433a47-8026-4811-860a-a8dd3e23b28b"
                    }
                ]
            }
        ],
        "https://dev-solution-spirit-cms.pantheonsite.io/rest/relation/node/article/uid": [
            {
                "_links": {
                    "self": {
                        "href": "https://dev-solution-spirit-cms.pantheonsite.io/user/1?_format=hal_json"
                    },
                    "type": {
                        "href": "https://dev-solution-spirit-cms.pantheonsite.io/rest/type/user/user"
                    }
                },
                "uuid": [
                    {
                        "value": "f2433a47-8026-4811-860a-a8dd3e23b28b"
                    }
                ],
                "lang": "en"
            }
        ],
        "https://dev-solution-spirit-cms.pantheonsite.io/rest/relation/node/article/field_image": [
            {
                "_links": {
                    "self": {
                        "href": "https://dev-solution-spirit-cms.pantheonsite.io/file/4?_format=hal_json"
                    },
                    "type": {
                        "href": "https://dev-solution-spirit-cms.pantheonsite.io/rest/type/file/image"
                    }
                },
                "uuid": [
                    {
                        "value": "25d66eec-ee67-4353-b4ef-0141a9b4858e"
                    }
                ],
                "lang": "en",
                "alt": null,
                "title": null,
                "width": 700,
                "height": 576
            }
        ]
    },
    "status": [
        {
            "value": true,
            "lang": "en"
        }
    ],
    "title": [
        {
            "value": "Updated Article",
            "lang": "en"
        }
    ],
    "created": [
        {
            "value": "2020-06-21T14:30:17+00:00",
            "lang": "en",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "changed": [
        {
            "value": "2020-07-21T15:59:12+00:00",
            "lang": "en",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "promote": [
        {
            "value": true,
            "lang": "en"
        }
    ],
    "sticky": [
        {
            "value": false,
            "lang": "en"
        }
    ],
    "default_langcode": [
        {
            "value": true,
            "lang": "en"
        }
    ],
    "revision_translation_affected": [
        {
            "value": true,
            "lang": "en"
        }
    ],
    "path": [
        {
            "alias": null,
            "pid": null,
            "langcode": "en",
            "lang": "en"
        }
    ],
    "comment": [
        {
            "status": 2,
            "cid": 0,
            "last_comment_timestamp": 1592749817,
            "last_comment_name": null,
            "last_comment_uid": 1,
            "comment_count": 0,
            "lang": "en"
        }
    ]
}

Update or PATCH article content type with image Rest API in Drupal 8.x
Update Article content with image via rest API Postman test

Awesome we have done it, At last just go to your admin panel and see inside content list you will find the updated content with image, isn’t it so easy πŸ˜‰

Congratulations now we can update or PATCH Article or any other content type with image as (optional) via rest API by JSON:API module in Drupal 8.x site πŸ™‚

Share your love with us πŸ˜‰

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Leave a Reply