filters, sorting, paging...
Field selection
- Sometimes donβt need all attributes of a resource. Give the API consumer the ability to choose returned fields. This will also reduce the network traffic and speed up the usage of the API.
GET /cars?fields=manufacturer,model,id,color
Paging (Slice)
- Add _offset and _limit (an X-Total-Count header is included in the response).
GET /cars?_offset=10&_limit=5
-
To send the total entries back to the user use the custom HTTP header: X-Total-Count.
-
Content-Range offset β limit / count.
-
offset: Index of the first element returned by the request.
-
limit: Index of the last element returned by the request.
-
count: Total number of elements in the collection.
-
-
Accept-Range resource max.
-
resource: The type of pagination. Must remind of the resource in use, e.g: client, order, restaurant, β¦
-
max : Maximum number of elements that can be returned in a single request.
Sorting
- Allow ascending and descending sorting over multiple fields.
- Use sort with underscore as
_sort. - In code, descending describe as
-, ascending describe as+.
GET /cars?_sort=-manufactorer,+model
Operators
- Add
_filterquery parameter and continue with field names,operations and values separated by,. - Pattern
_filter=<fieldname><operation><value>. - Supported operations.
=equal!=not equal<less<=less or equals>greater>=greater or equals~=like|=in (values must be separated with|
GET http://127.0.0.1:8080/robe/users?_filter=name=seray,active=true
Full-text search
- Add
_q.
GET /cars?_q=nissan
I got error after some queries and application crashed.
Error Detail :
Some(Queries { fields: [], filter: [], q: None, paginate: (OFFSET(0), LIMIT(5)), slice: [], sort: [] })
paging LIMIT(5)
Some(Queries { fields: [], filter: [], q: None, paginate: (OFFSET(0), LIMIT(5)), slice: [], sort: [] })
thread 'main' panicked at 'called Result::unwrap() on an Err value: ErrorImpl { code: EofWhileParsingValue, line: 1, column: 0 }', src/libcore/result.rs:860
note: Run with RUST_BACKTRACE=1 for a backtrace.
I checked the issue. It's ok about selection and paging.
Selection Test: http://localhost:8081/posts?_fields=author π http://localhost:8081/posts?_fields=author,tags π
Paging Tests:
OK
- http://localhost:8081/posts?_limit=1 π
- http://localhost:8081/posts?_offset=1 π
- http://localhost:8081/posts?_offset=1&_limit=1 π
Selection and Paging Tests
- http://localhost:8081/posts?_fields=author&_limit=1 π
- http://localhost:8081/posts?_fields=author,tags&_limit=1 π
- http://localhost:8081/posts?_fields=author&_offset=1 π
- http://localhost:8081/posts?_fields=author,tags&_offset=1 π