Fetching resources (GET)
This page shows examples of various GET requests for the JSON:API module.
In all examples below, no request headers are required. No authentication is required if anonymous users can access content entities. For config entities like menu's see last section(s).
Note that in all cases, when an id is needed, it is always the entity's uuid, not the entity id.
Accept header
Make sure you always send the accept header: Accept: application/vnd.api+json.
curl \
--header 'Accept: application/vnd.api+json' \
....
Basic GET example
URL: http://example.com/jsonapi/node/article/{{article_uuid}}
Response
HTTP 200 response. The response body contains the JSON:API object of a single article node, including attributes, relationships, and links.
GET multiple articles
URL: http://example.com/jsonapi/node/article
Response
HTTP 200 response. The response body contains the JSON:API object of max. 50 articles, including link to next.
GET first 10 articles
URL: http://example.com/jsonapi/node/article?page[limit]=10
Response
JSON response of max. 10 articles, including links to next.
GET second page of 10 articles
URL: http://example.com/jsonapi/node/article?page[limit]=10&page[offset]=10
Response
HTTP 200 response. The response body contains the JSON:API object of max. 10 articles, including links to prev, next, etc.
For more information about pagination, see: https://www.drupal.org/docs/8/modules/jsonapi/pagination
GET multiple articles sorted
URL: http://example.com/jsonapi/node/article?sort=nid
Response
HTTP 200 response. The response body contains the JSON:API object of articles, sorted by nid ascending. Use &sort=-nid
to sort descending.
For more information about sorting, see: https://www.drupal.org/node/2803141
GET article filtered by title
URL: http://example.com/jsonapi/node/article?filter[article-title][path]=title&filter[article-title][value]={{title_filter}}&filter[article-title][operator]==
Response
HTTP 200 response. The response body contains the JSON:API object of articles, filtered by 'title' field value matching the value '{{title_filter}}'
For more information about filters, see: https://www.drupal.org/node/2943641
GET article media entity reference field_image url, uri by including references
Newer syntax
URL:
http://example.com/jsonapi/node/article/{{article_uuid}}?include=field_image&fields[file--file]=uri,url
Older syntax
URL: http://example.com/jsonapi/node/article/{{article_uuid}}?include=field_image,field_image.image,field_image.image.file--file&fields[field_image]=image&fields[file--file]=uri,url
Response
HTTP 200 response. The response body contains the JSON:API object of included media image relationships, matching the single article node by '{{article_uuid}}'
GET article along with complete related data set (author, taxonomy term, etc.)
URL: http://example.com/jsonapi/node/article?fields[node--article]=uid,title,created&include=uid
Response
HTTP 200 response. The response body includes a user object containing all information about the associated user. The same syntax will work for other related data such as taxonomy term.
GET article with user reference field, field_user_ref_example, and its profile, ref_example_profile, (reference field inside reference field)
URL: http://example.com/jsonapi/node/article/{{article_uuid}}?include=field_user_ref_example,field_user_ref_example.ref_example_profile
Response
HTTP 200 response. The response body includes a user object containing all information about the associated user and its profile. The same syntax will work for other related data that also contains a reference field.
GET article along with selected related data items (author, taxonomy term, etc.)
URL: http://example.com/jsonapi/node/article?fields[node--article]=uid,title,created&fields[user--user]=name,mail&include=uid
Response
HTTP 200 response. The response body includes a user object containing the specified fields in the related object (in this case, author name and author email). The same syntax will work for other related data such as taxonomy term.
GET user accounts
URL: http://example.com/jsonapi/user/user?filter[anon][condition][path]=uid&filter[anon][condition][value]=0&filter[anon][condition][operator]=<>
Response
HTTP 200 response. The response body contains the JSON:API object of user accounts in the system, excluding the anonymous user account. Note that if you want to get a list of all user accounts, you must use the above query, as simply issuing a GET request to /jsonapi/user/user
will result in a HTTP 403 error.
Getting config entities.
As config entities (menu, node type, tour) are not equal to content entities (nodes, users) this is a little more tricky. Currently config entities are read only.
For easy testing we use user-1 and the basic_auth module.
- Enable basic auth module
- Assume the user-1 name is admin and password is admin
Using the following command will list menu's.
curl \
--header 'Accept: application/vnd.api+json' \
http://admin:admin@drupal.d8/jsonapi/menu/menu
Article from Drupal Documentation.