Use Accept Headers to GET XML Content as a preference
How to complete the challenge GET /todos XML (200) to successfully GET all the todos in XML format as first preference.
GET /todos XML Preference (200)
It is possible to ask for multiple types in the Accept header, expressing a preference for the returned format. If none of the types are available then expect a 406 response.
Issue a GET request on the
/todosend point with anAcceptheader ofapplication/xmlfollowed byapplication/jsonto receive results in XML format if supported, or JSON if not.
GETrequest will receive a response with all the todo items- e.g.
GET /todosto get all the todo items
- e.g.
200is a success code, in this case it means the end point exists and the todo items were returnedAcceptmeans that anAcceptheader was added to specify that the todos should be returned in XML format as first preference, followed by JSON as second preference- add the
X-CHALLENGERheader to track progress
Basic Instructions
- Issue a
GETrequest to end point "/todos"- if running locally that endpoint would be
https://apichallenges.com/todos
- if running locally that endpoint would be
- The request should have an
Acceptheader specifying XML format by using a value ofapplication/xml,application/json - The request should have an
X-CHALLENGERheader to track challenge completion - The response status code should be
200when all the details are valid. - Check the body of the message has JSON format data, which is the default from the server
- Check the
content-typeheader in the response hasapplication/json
The chained Accept header application/xml,application/json asks for XML as first preference, but if not supported then supply JSON. Because no q values are supplied, both media types behave as if they had q=1.0, so order is used to choose between equally preferred supported formats.
Try it now
GET /todos with XML preferred over JSON
Example Request
> GET /todos HTTP/1.1
> Host: apichallenges.com
> User-Agent: rest-client
> X-CHALLENGER: x-challenger-guid
> Accept: application/xml,application/json
Example Response
< HTTP/1.1 200 OK
< Connection: close
< Date: Sat, 29 May 2021 10:05:24 GMT
< Content-Type: application/xml
< 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:
<todos>
<todo>
<doneStatus>false</doneStatus>
<description/>
<id>267</id>
<title>train staff</title>
</todo>
<todo>
<doneStatus>false</doneStatus>
<description/>
<id>268</id>
<title>schedule meeting</title>
</todo>
</todos>
Overview Video
Lessons Learned
Accept: application/xml,application/jsonexpresses a preference order withoutqvalues.- Negotiation should return
application/xmlwhenXMLis supported, not simply the API default. - Preference lists are useful for clients that can process a fallback format.
- A supported lower-priority format still matters when a higher-priority format is unavailable.
Suggested Experiments
- Reverse the header to
Accept: application/json,application/xmland observe whichContent-Typewins. - Add
qvalues such asapplication/xml;q=0.5, application/json;q=1and compare with the comma-order request. - Try
Accept: application/problem+xml, application/xml;q=0.5and confirm that the supportedXMLfallback can still be used.