Skip to content

Instantly share code, notes, and snippets.

@jonurry
Created April 18, 2018 16:10
Show Gist options
  • Save jonurry/a002d723f307e697304de721d1b43f6f to your computer and use it in GitHub Desktop.
Save jonurry/a002d723f307e697304de721d1b43f6f to your computer and use it in GitHub Desktop.
18.1 Content Negotiation (Eloquent JavaScript Solutions)
let aboutURL = 'https://eloquentjavascript.net/author';
let mediaTypes = [
'text/plain',
'text/html',
'application/json',
'application/rainbow+unicorns'
];
async function getResponsesForMediaType(url, type) {
let r = await fetch(url, {headers: {'accept': type}});
r = await r.text();
console.log(`\n---------- type: ${type} ----------\n`, r);
};
for (let type of mediaTypes) {
getResponsesForMediaType(aboutURL, type);
};
@jonurry
Copy link
Author

jonurry commented Apr 18, 2018

Hints

Base your code on the fetch examples earlier in the chapter.

Asking for a bogus media type will return a response with code 406, “Not acceptable”, which is the code a server should return when it can’t fulfil the Accept header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment