Brief review
Using fetch with public APIs
CP3 out now
fetch
to fetch data from a serverfetch(URL).then(...).then(...).catch(...)
pipeline
to handle requestsfetch
template
When we make a call to fetch data (whether it's HTML, txt, JSON, etc) we use a
function in JavaScript called fetch
, which takes a URL path to
fetch data from.
Here's the general template you will use for most AJAX code:
const BASE_URL = ""; // you may have more than one
// Step 1. Write a function to "fetch" data from a URL
function makeRequest() {
let url = BASE_URL + "?query0=value0"; // some requests require parameters
fetch(url)
.then(checkStatus)
//.then(resp => resp.text()) // use this if your data comes in text
//.then(resp => resp.json()) // or this if your data comes in JSON
.then(processData)
.catch(handleError); // define a user-friendly error-message function
}
// Step 2: Implement a function to process the response data. You may want to break this apart
// into multiple functions.
function processData(responseData) {
// Do something with your responseData! (build DOM, display messages, etc.)
}
// Step 3. Include the checkStatus function
function checkStatus(response) {
if (response.ok) {
return response;
} else {
throw Error("Error in request: " + response.statusText);
}
}
An application programming interface (API) is a communication protocols that allows two pieces of software to communicate.
A Web API is a set of pre-defined URLs with parameters that allow a user to get information from a web server. Can be used by many types of clients.
Source: https://xkcd.com/869
Source: https://en.wikipedia.org/wiki/Client%E2%80%93server_model
Much like the request/response analogy at a restaurant!
To make a request to a web service (API), we need to know:
Consider the following request we might make with fetch
:
function makeRequest() {
let requestString = "https://api.nasa.gov/planetary/apod?" +
"api_key=DEMO_KEY&date=2018-09-30";
fetch(requestString)
.then(checkStatus) // 1
.then((resp) => resp.json()) // 2
.then(processData) // 3
.catch(handleError); // 4
}
We're about to dive into how to get started with a public API. The first thing you usually want to do is to find a url with query parameters you're interested in, and see if you can view it on your browser.
There is a great Chrome extension to format nested JSON returned by different API's!
Chrome Extension: JSON View
The following slides provide a few examples of breaking down request URLS for various APIs, starting with the NASA APOD API.
Solution: apod.html
Examples:
Note: Go "down" slides for more
An word-querying API: https://www.datamuse.com/api/
Examples:
Note: Go "down" slides for more
"Documentation": https://xkcd.com/json.html
Examples:
Comic of the Day: https://xkcd.com/info.0.json
Many sites and APIs you try to use will give you an error about CORS.
For example, we used the Elephants API earlier just by opening the API endpoint in the browser.
Try doing a fetch with it!
Security measure browsers have implemented that restricts one site from accessing resources in another unless explicitly given permission.
Most of the APIs we've chosen for CP3 or demo'd today have explicitly allowed *
, meaning anyone.
Many APIs you run into around the internet will be more locked down than this.