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/todosend point with anAcceptheader ofapplication/gzipto receive a response with a406'NOT ACCEPTABLE' status code.
GETrequest asks for a response with all the todo items- e.g.
GET /api/todosto get all the todo items
- e.g.
406is a success code, in this case it means the accept header is not supported by the system- add the
X-CHALLENGERheader to track progress
Basic Instructions
- Issue a
GETrequest to end point "/api/todos"- if running locally that endpoint would be
https://apichallenges.com/api/todos
- if running locally that endpoint would be
- The request should have an
Acceptheader with the valueapplication/gzip - The request should have an
X-CHALLENGERheader to track challenge completion - The response status code should be
406when all the details are valid. - Check the body of the message has JSON formatted error message response
- Check the
content-typeheader in the response hasapplication/jsonmatching 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
Lessons Learned
406 Not Acceptableproves the route exists but cannot produce the requested media type.- Unsupported
Acceptvalues are content negotiation failures, not missing-resource failures. - Negative negotiation tests help expose whether an API ignores client preferences.
- Structured
+jsonvalues still need explicit support and are not aliases for plainapplication/json.
Suggested Experiments
- Change
Accept: application/gziptoAccept: application/xmland compare406 Not Acceptablewith a successful representation. - Try
Accept: application/json;q=0, application/xml;q=0to see whether refusing all normal todo formats leaves the server with no acceptable representation. - Try
Accept: application/problem+jsonorAccept: application/*+jsonand compare structuredJSONrejection with plainapplication/json.