How to complete the challenge GET /api/todos (200) ? filter and _sortBy

How to issue a GET request on a top level entity endpoint, filter the returned todos, and sort the filtered results.

GET /api/todos (200) ? filter and _sortBy

Issue a GET request on the /api/todos end point with a query filter and a query parameter to sort the filtered todos, requesting the response in JSON format.

  • todo fields can be used as URL parameters to filter collection results
  • _sortBy is the URL parameter used to sort collection results
  • prefix a sort field with - to sort descending
  • for example, doneStatus=false&_sortBy=-id returns only not done todos, sorted by id from highest to lowest
  • ensure header Accept: application/json exists so the filtered and sorted todo collection 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 doneStatus=false and _sortBy=-id as URL parameters:
    • https://apichallenges.com/api/todos?doneStatus=false&_sortBy=-id
  • The response status code should be 200 because the request is accepted
  • Check that all returned todos have doneStatus set to false, and that the ids are descending

Try it now

GET /api/todos?doneStatus=false&_sortBy=-id to filter and sort todos

Example Request

> GET /api/todos?doneStatus=false&_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": "not done with highest id",
      "doneStatus": false,
      "description": ""
    },
    {
      "id": 3,
      "title": "not done with lower id",
      "doneStatus": false,
      "description": ""
    }
  ]
}

Lessons Learned

  • Combining a filter with _sortBy tests operation order: narrow the set, then order the survivors.
  • Multi-parameter queries need assertions for both inclusion and ordering.
  • Sorting a filtered subset can hide missed data if you only inspect the first item.

Suggested Experiments

  • Sort the filtered results by -title and verify every returned todo still matches doneStatus.
  • Add one done and one not-done fixture with similar titles to prove sorting does not override filtering.
Experiment with this endpoint