Tutorial 7
CSCI 344: Building a “Vanilla” JavaScript Client
Spring 2025
Announcements
Today’s Plan
1. Set Up
You are now ready to begin your HW03. Please view your index.html file in live server to verify that it works.
2. Authentication
For many web applications, different information / capabilities are available to the user depending on who is logged into the system. The authentication workflow is as follows:
HTTP Request / Response
Originates from the Client
+------------------------------+
| HTTP Request |
+------------------------------+
| Method: GET |
| URL: /api/resource | HEADER
| Host: example.com |
| Authorization: Bearer TOKEN |
| Accept: application/json |
| User-Agent: MyApp/1.0 |
+------------------------------+
| No payload for GET requests | BODY�+------------------------------+
Response from the Server
+--------------------------------+
| HTTP Response |
+--------------------------------+
| Status: 200 OK | HEADER
| Content-Type: application/json |
+--------------------------------+
| { |
| "id": 123, |
| "name": "Private info", | BODY
| "username": "webdev" |
| } |
+--------------------------------+
3. Fetch data from the API
Our first coding task will be to fetch the user’s feed from the API.
Let’s copy the sample JS code and adjust it. Recall the basic pattern for fetching data from a server
�async function getPosts() {
const url = "https://photo-app-secured.herokuapp.com/api/posts/?limit=3";
const response = await fetch(url);
const data = await response.json();
console.log(data);
}
3. Fetch data from the API
The only new thing is that we’re adding some additional HTTP headers:
async function getPosts() {
const url = "https://photo-app-secured.herokuapp.com/api/posts/?limit=3";
const response = await fetch(url,
{
method: "GET",
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <ACCESS_TOKEN>'
}
});
const data = await response.json();
console.log(data);
}
HTTP Header Information
Task 1: Implement Post Panel w/Live Data
Let’s try implementing the Post Panel….
Task 2: Implement “Add Bookmark”
Let’s try implementing the “Add Bookmark” functionality….
How do we know how to add a bookmark using the REST API?
Task 3: Implement “Remove Bookmark”
Let’s try implementing the “Remove Bookmark” functionality….
How do we know how to add a bookmark using the REST API?