Have you ever wondered how Facebook is able to automatically display your Instagram photos?
Have you ever wondered how the Starbucks store locator gives you directions to the closest Starbucks? It asks Google Maps!
As a programmer, you don't want to install a database of all the world’s addresses and algorithms in order to search them on your server. (hint: just use Google's API)
You can’t use the card catalog yourself because you don’t have access and you don’t know how to use it.
Your website doesn’t know how to access or read the Google Books database, but it can use the Google Books API to request the book.
Librarian | API |
---|---|
Human Language (like English) | HTTP protocol |
Question containing details (title and author) | Request containing parameters (title and author) |
Answer | JSON response including URL |
2. Public Data: There is a huge amount of data available, and APIs make that data available to use in a structured way.
3. User Data: We also use APIs to access data from other products and integrate them with our own. Users use multiple products and expect for them to work together - synergy.
This is all dynamic content.
We need to understand:
REST APIs conform to a set of constraints.
Read this article.
Then let's watch the first 6 minutes of
this video!
JSON = JavaScript Object Notation
{
"firstName": "Gene",
"lastName": "Smith",
"address": {
"streetAddress": "425 2nd Street",
"city": "San Francisco",
"state": "CA",
"postalCode": 94107
},
"phoneNumbers": [
"212 732-1234",
"646 123-4567" ]
}
Your product manager provided you the following UI design. How would you create a JSON from this page?
GET and POST Requests
Your browser will automatically make a GET request, and this particular response will be JSON. This is very common for when you are making API calls to get data.
Click here for an example.
The GET request on the prior slide yields JSON in raw form.
To make the JSON more readable, we will need to parse it.
Click here to add JSON Formatter,
an extension from the Chrome Web Store.
Then click between Raw and Parsed. Notice what happens!
Web apps can be set up to make many, many GET and POST requests for almost any user action.
Frontend developers send those requests and receive the responses.
Frontend developers also determine WHEN a request is sent and how it is handled by the UI.
Asynchronous technologies enable users to access information or communicate at different points of time, usually at the time of choice of the user.
A traditional data-driven web application is written so that it:
This process has a negative effect....
As a result, the user has "down time" - a period of time where the users can't interact with the webpage at all.
There are different methods for making async requests, and new ones are being added all the time.
We will talk about a tool that is built into JavaScript, called Fetch.
The Fetch API allows JS to access and manipulate the HTTP pipeline, such as requests and responses, using fetch()
.
Let's take a look at "GET
" and "POST
"
//GET
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
//POST
let data = { name: "lhack"}
fetch('http://example.com/movies.json', {
method: 'POST',
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data));
//GET
fetch('http://example.com/movies.json')
//POST
let data = { name: "lhack"}
fetch('http://example.com/movies.json', {
method: 'POST',
body: JSON.stringify(data)
})
Notice that both requests have a url string as a parameter.
The POST request has an additional parameter: an object.
The object in the POST request has 2 properties:
method
, which must have a value of 'POST'body
, which must utilize the JSON.stringify() method.
When using fetch()
, expect a response
and data
.
Let's look at "GET
" and "POST
" again.
//GET
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
//POST
let data = { name: "lhack"}
fetch('http://example.com/movies.json', {
method: 'POST',
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data));
Notice we can use method chaining and apply .then()
right after we use the fetch()
method.
Let's walk through this example
We will check out this example using the Cat Facts API
Click here
document.addEventListener("click", function() {
let text = document.getElementById("paragraph-text")
text.textContent = 'loading . . .'
fetch('https://cat-fact.herokuapp.com/facts')
.then(response => response.json())
.then(data =>
for (let i = 0; i < data.length; i++) {
text.textContent += data[i].text
}
);
});
Hint: Take a look at the structure of the JSON data to complete the exercise.
Ask a user for a character name, then make an API call to fetch and then display the character's thumbnail, name, and description to the user.
Click here
for the instructions and links to resources to help you sign up for
an API key, use fetch()
.
Watch these Web API videos.
Thank you for your attention!