Drupal JSON:API GET, POST, PATCH and DELETE Samples

Read Article
Pause
Resume
Stop
Drupal JSON:API GET, POST, PATCH and DELETE Samples

The API that the JSON:API module makes available is centered on Drupal's entity types and bundles. Every bundle receives its own, unique URL path, which all follow a shared pattern

HTTP Methods

JSON:API specifies what HTTP Methods to accept. Those are: GET, POST, PATCH, and DELETE. Notably, PUT is not included.

  • GET - Retrieve data, can be a collection of resources or an individual resource
  • POST - Create a new resource
  • PATCH - Update an existing resource
  • DELETE - Remove an existing resource

Authentication

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

Headers

Make sure to use 'Content type' and 'Accept' headers when appropriate

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

Response codes

The JSON:API Specification also dictates acceptable responses. The Drupal implementation uses a subset of those. The module can respond with the following codes:

  • 200 OK - All successful GET and PATCH requests
  • 201 Created - All successful POST requests (response includes the newly created resource)
  • 204 No Content - All successful DELETE requests

And here's the detailed documentation - https://www.drupal.org/docs/8/modules/jsonapi/get-post-patch-and-delete

And here's the sample postman collection - https://gitlab.com/heykarthikwithu/drupal-jsonapi---sample-api-calls

Thanks :)

Comments

Scroll to Top
We Need Your Consent
By clicking “I Accept Cookies”, you agree to the storing of cookies on your device to enhance site navigation & analyze site usage.
I Accept Cookies