How to complete the challenge GET /api/todos (200) ? _sortBy descending
How to issue a GET request on a top level entity endpoint and sort the returned todos in descending order.
GET /api/todos (200) ? _sortBy descending
Issue a GET request on the
/api/todosend point with a query parameter to sort todos descending by a field, requesting the response in JSON format.
_sortByis the URL parameter used to sort collection results- prefix a field with
-to sort descending - for example,
_sortBy=-idsorts the todos from highest id to lowest id - use a field that exists on a todo, such as
id,title,doneStatus, ordescription - if the response is not JSON then add header
Accept: application/jsonso the returned todo order is visible as JSON
Basic Instructions
- Issue a
GETrequest to end point "/api/todos"https://apichallenges.com/api/todos
- The request should have an
X-CHALLENGERheader to track challenge completion - The request should have an
Accept: application/jsonheader so the API returns todos in JSON format - Add
_sortBy=-idas a URL parameter:https://apichallenges.com/api/todos?_sortBy=-id
- The response status code should be
200because the request is accepted - Check that the returned todos are ordered by
idfrom highest to lowest
Try it now
GET /api/todos?_sortBy=-id to sort todos by id descending
Example Request
> GET /api/todos?_sortBy=-id HTTP/1.1
> Host: apichallenges.com
> User-Agent: rest-client
> X-CHALLENGER: x-challenger-guid
> Accept: application/json
Example Response
< HTTP/1.1 200 OK
< Connection: close
< Content-Type: application/json
< X-Challenger: x-challenger-guid
Returned body:
{
"todos": [
{
"id": 10,
"title": "highest id first",
"doneStatus": false,
"description": ""
},
{
"id": 9,
"title": "then the next id",
"doneStatus": true,
"description": ""
}
]
}
Lessons Learned
_sortBy=-idreverses collection order and is useful for newest-item style views.- Descending sort should be verified across every adjacent pair in the response.
- Sort direction bugs can pass if the dataset has only one item.
Suggested Experiments
- Compare
_sortBy=-idwith_sortBy=+idand confirm the response order is reversed. - Create a new todo, then sort by
-idto check whether it appears near the start.