We define our url pattern as following.
We would suggest all api route pattern use following patterns. It is the world wide best practice.
GET /posts
GET /posts/1
POST /posts
PUT /posts/1
PATCH /posts/1
DELETE /posts/1
GET /profile
POST /profile
PUT /profile
PATCH /profile
we use standard url query format to pass array data.
a=1&a=2
Use .
to access deep properties
GET /posts?title=json-server&author=typicode
GET /posts?id=1&id=2
GET /comments?author.name=typicode
Use _offset
and optionally _limit
to paginate returned data. (an X-Total-Count
header is included in the response)
In the Link
header you'll get first
, prev
, next
and last
links.
GET /posts?_offset=10
GET /posts?_offset=7&_limit=20
note: 10 items are returned by default
Add _sort
and _order
(ascending order by default)
# asc
GET /posts?_sort=views
# desc
GET /posts/1/comments?_sort=-votes
note: list posts by views ascending order and comments by votes descending order
For multiple fields, use the following format:
GET /posts/1/comments?_sort=-votes&_sort=likes
_prefixing a path with -
will flag that sort is descending order.
When a path does not have the -
prefix, it is ascending order.
Add _gt
, _lt
, _gte
or _lte
for getting a range
GET /posts?views_gte=10&views_lte=20
Add _ne
to exclude a value
GET /posts?id_ne=1
Add _like
to filter (RegExp supported)
_like
support array
GET /posts?title_like=server
_not
means not like
GET /statistics/post?title_not=server
If a field is an array, like:
assignees=*
means assignees has at least one member.assignees=none
means assignees is an empty array.
Add q
GET /posts?q=internet
Specifies which document fields to include or exclude
GET /posts?_select=title&_select=body
GET /posts?_select=-comments&_select=-views
or
_select=title,body
prefixing a path with -
will flag that path as excluded.
When a path does not have the -
prefix, it is included
A projection must be either inclusive or exclusive.
In other words, you must either list the fields to include (which excludes all others),
or list the fields to exclude (which implies all other fields are included).
support groupBy some field
suggestion: Do not mix analytic api and business api
GET /statistics/post?_group=author
support to populate reference
GET /statistics/post?_populate=author
without populate
{
title: "a",
author: "12345",
}
with populate
{
title: "a",
author: {
id: "12345",
name: "aaa",
},
}