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/todos end point with a query parameter to sort todos descending by a field, requesting the response in JSON format.

  • _sortBy is the URL parameter used to sort collection results
  • prefix a field with - to sort descending
  • for example, _sortBy=-id sorts the todos from highest id to lowest id
  • use a field that exists on a todo, such as id, title, doneStatus, or description
  • if the response is not JSON then add header Accept: application/json so the returned todo order is visible as JSON

Basic Instructions

  • Issue a GET request to end point "/api/todos"
    • https://apichallenges.com/api/todos
  • The request should have an X-CHALLENGER header to track challenge completion
  • The request should have an Accept: application/json header so the API returns todos in JSON format
  • Add _sortBy=-id as a URL parameter:
    • https://apichallenges.com/api/todos?_sortBy=-id
  • The response status code should be 200 because the request is accepted
  • Check that the returned todos are ordered by id from 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=-id reverses 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=-id with _sortBy=+id and confirm the response order is reversed.
  • Create a new todo, then sort by -id to check whether it appears near the start.
Experiment with this endpoint