Learn about JSON: API Module in Drupal 8

Tram Ho

Preamble

Today I will introduce to everyone a popular module of Drupal. It is JSON: API Module.
The JSON API is a tool that helps format your JSON responses. By following the shared conventions, you can increase performance, take advantage of the general tool, and focus on your application.
Clients can take advantage of its efficient cache, sometimes eliminating network requests altogether.

Resource structure

Here is the resource structure of JSON API:

jsonapi : Contains information, version of module JSON: API in use.
attributes : store specific values ​​for a basic Resource. For example: title, body, created time, langcode, …
relationships : are values ​​that belong to another Resource in the system. Relationships often represent values ​​stored by an entity reference.

Fetching resources (GET)

In this section, we learn how to access one or more resources of content type as article .
1. Get all resources:
URL : http: // {host} / jsonapi / node / article
Method : GET Response :
Statuscode : 200 OK
Body : Contains all JSON: API objects of the article node.

We can test on Postman:

2. Get resources by uuid:
URL : http: // {host} / jsonapi / node / article / {{article_uuid}}
Method : GET
Response :
Statuscode : 200 OK
Body : Contains JSON object: API has article_uuid which is identical to article_uuid passed.
We can test on Postman:

Creating new resources (POST)

In this section, we learn how to create a new Resource whose content type is article .
First, we need to go to admin / config / services / jsonapi and select “Accept all JSON: API create, read, update, and delete operations.” .


Usually some form of authentication is used for POST requests. All my examples use Basic Authentication.
Turn on Drupal’s HTTP Basic Authentication Module, set the API user (and role) permissions and set the encrypted username and password to the ‘Authorization’ request header.

We can test it on Postman:

  • Header :
    • Accept : application / vnd.api + json
    • Content-Type : application / vnd.api + json
    • Authorization : Basic dnVvbmduZ286MTIzNDU2Nzh4QFg =
  • URL : http: // {host} / jsonapi / node / article
  • Method : POST
  • Request body :

  • Response :
    • Statuscode : 201 (created)
    • Body : Contains the JSON: API object of the created article

Updating existing resources (PATCH)

In this section, we learn how to update an existing Resource.
We can test it on Postman:

  • Header :
    • Accept : application / vnd.api + json
    • Content-Type : application / vnd.api + json
    • Authorization : Basic dnVvbmduZ286MTIzNDU2Nzh4QFg =
  • URL : http: // {host} / jsonapi / node / article / {{article_uuid}}
  • Method : PATCH
  • Request body :

  • Response :
    • Statuscode : 200 (OK)
    • Body : Contains the JSON: API object of the article that has been updated

Removing existing resources (DELETE)

In this section, we learn how to delete an existing Resource.

We can test it on Postman:

  • Header :
    • Accept : application / vnd.api + json
    • Content-Type : application / vnd.api + json
    • Authorization : Basic dnVvbmduZ286MTIzNDU2Nzh4QFg =
  • URL : http: // {host} / jsonapi / node / article / {{article_uuid}}
  • Method : DELETE
  • Response :
    • Statuscode : 204 No content
    • Body : empty
Share the news now

Source : Viblo