Brief review of Promises/AJAX
Using fetch with public APIs
CP3 out now, Midterm Monday
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) { // response.status >= 200 && response.status < 300
throw Error("Error in request: " + response.statusText);
}
return response;
}
JS
We initiate a fetch
of a URL
A fetch
returns a Promise
Promises are good because...
fetch
We won't be constructing Promises, but will be using them when we fetch
information
from a server, which is an uncertain task
The returned Promise
contains a value that is a Response
from the
requested resource, which we can get then.
after the fetch call.
fetch(url).then((resp) => {
// resp is a Response object!
});
JS
What's in a Response
?
Response
objectProperty | Description |
---|---|
response.status | Status code (e.g. 200, 400, etc.) |
response.ok | Whether the response has a success (2XX) status code, short for: response.status > 200 && response.status <= 300) |
response.statusText | Status text returned in the response (e.g. "200 OK", "400 Bad Request", "503 Service Unavailable") |
response.json(), response.text() | Methods to extract the response body depending on the data format |
We will use the Response returned (in a Promise) from fetch
to:
response.ok
catch
statement. It's good
to construct the Error with details about the response.statusText
response.text()
or response.json()
Set by the server
A 2XX status code (e.g. 200, 201, etc.) will indicate a successful request
Other status codes indicate a failure in the request
Full list here
We can access the status code and status text with the Response object
This is a useful service to test different error codes: https://httpstat.us/
Demo: status-tester.html
Recall our earlier Cafe Demo
Version 1 (HTML): Menu in HTML
Version 2 (JS): Menu in JSON as local variable in JS
Version 3: Let's use fetch with the JSON on a server!
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
}
JS
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.