How to complete the challenge GET /api/todos (200) ? _sortBy ascending
How to issue a GET request on a top level entity endpoint and sort the returned todos in ascending order.
GET /api/todos (200) ? _sortBy ascending
Issue a GET request on the
/api/todosend point with a query parameter to sort todos ascending by a field, requesting the response in JSON format.
GETrequest will return items from the/api/todosend point200is the success code meaning the request was accepted_sortByis the URL parameter used to sort collection results- use a todo field name as the value, e.g.
_sortBy=title - a plain field name sorts ascending
+fieldalso means ascending, but using just the field name avoids client URL encoding surprises- ensure header
Accept: application/jsonexists so the filtered and sorted todo collection 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=titleas a URL parameter:https://apichallenges.com/api/todos?_sortBy=title
- The response status code should be
200because the request is accepted - Check that the returned todos are ordered by
titlefrom A to Z
Try it now
GET /api/todos?_sortBy=title to sort todos by title
Example Request
> GET /api/todos?_sortBy=title 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": 4,
"title": "file paperwork",
"doneStatus": false,
"description": ""
},
{
"id": 2,
"title": "process payroll",
"doneStatus": true,
"description": ""
}
]
}
Lessons Learned
_sortBy=+idmakes ascending order explicit instead of relying on a default.- Sorting tests should compare adjacent values, not just the first and last record.
- A stable sort gives clients predictable collection browsing.
Suggested Experiments
- Sort by
+titleand compare alphabetic order with+id. - Add a new todo and confirm its position is based on the sorted field, not creation time alone.