How to complete the challenge GET /api/todos (406)

Some APIs will report an error when asked for a return format that they do not support. Other APIs will respond with a default. The API Challenges will respond with a 406 status code. We can test for this by sending a GET request for all todos but pass in an 'accept' format that the system does not support.

GET /api/todos (406)

When we issue a request with an accept header, we are asking for a specific content format in the response. But... if we ask for a format that is not supported then the system may respond with a 406 'NOT ACCEPTABLE' status code.

Issue a GET request on the /api/todos end point with an Accept header of application/gzip to receive a response with a 406 'NOT ACCEPTABLE' status code.

  • GET request asks for a response with all the todo items
    • e.g. GET /api/todos to get all the todo items
  • 406 is a success code, in this case it means the accept header is not supported by the system
  • add the X-CHALLENGER header to track progress

Basic Instructions

  • Issue a GET request to end point "/api/todos"
    • if running locally that endpoint would be
      • https://apichallenges.com/api/todos
  • The request should have an Accept header with the value application/gzip
  • The request should have an X-CHALLENGER header to track challenge completion
  • The response status code should be 406 when all the details are valid.
  • Check the body of the message has JSON formatted error message response
  • Check the content-type header in the response has application/json matching the response body

Some systems may simply ignore the 'accept' header and return the response in the default format.

Try it now

GET /api/todos with an unsupported Accept header to trigger 406

Example Request

> GET /api/todos HTTP/1.1
> Host: apichallenges.com
> User-Agent: insomnia/2021.2.2
> Accept: application/gzip
> X-CHALLENGER: x-challenger-guid

Example Response

< HTTP/1.1 406 Not Acceptable
< Connection: close
< Date: Sat, 17 Jul 2021 12:21:37 GMT
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: *
< X-Challenger: x-challenger-guid
< Server: Jetty(9.4.z-SNAPSHOT)
< Via: 1.1 vegur

Example Response body:

{
  "errorMessages": [
    "Unrecognised Accept Type"
  ]
}

Overview Video

Watch on YouTube: "Solution to Get all Todos in usupported format"

Patreon ad free version

Lessons Learned

  • 406 Not Acceptable proves the route exists but cannot produce the requested media type.
  • Unsupported Accept values are content negotiation failures, not missing-resource failures.
  • Negative negotiation tests help expose whether an API ignores client preferences.
  • Structured +json values still need explicit support and are not aliases for plain application/json.

Suggested Experiments

  • Change Accept: application/gzip to Accept: application/xml and compare 406 Not Acceptable with a successful representation.
  • Try Accept: application/json;q=0, application/xml;q=0 to see whether refusing all normal todo formats leaves the server with no acceptable representation.
  • Try Accept: application/problem+json or Accept: application/*+json and compare structured JSON rejection with plain application/json.
Experiment with this endpoint