Skip to content

Commit 8524380

Browse files
committed
first
1 parent 1ce8253 commit 8524380

File tree

299 files changed

+48088
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

299 files changed

+48088
-2
lines changed

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1-
# fcc-learn-http-assets
1+
# Assets for "Learn HTTP" on FreeCodeCamp
22

3-
This is a snapshot of the code samples for the "Learn HTTP" course on Boot.dev at the time the video for FreeCodeCamp was released on YouTube.
3+
This is a snapshot of the code samples for the ["Learn HTTP" course](https://boot.dev/courses/learn-http) on [Boot.dev](https://boot.dev) at the time the video for FreeCodeCamp was released on YouTube. If you want the most up-to-date version of the code, please visit the official [Boot.dev course](https://boot.dev/courses/learn-http). Otherwise, if you're looking for the files used in the video, you're in the right place!
4+
5+
* [Course code samples](/course)
6+
* [Project steps](/project)
7+
8+
## A note on the API used in the course coding assignments
9+
10+
The API used throughout the course is a *live* API hosted on Boot.dev. There is a *chance* that the API will change in the future, which will of course break any code that interacts with it, but it's unlikely. If it does change, I'll update this repo with an explanation.
11+
12+
The base URL of the API is: `https://api.boot.dev/v1/courses_rest_api/learn-http/`
13+
14+
## Alternative APIs you can practice with
15+
16+
If you want to practice writing client-side HTTP code against a live API, here are some alternative options I would recommend:
17+
18+
* [PokeAPI](https://pokeapi.co/)
19+
* [Star Wars API](https://swapi.dev/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const apiKey = generateKey()
2+
const items = await getItemData()
3+
4+
logItems()
5+
6+
// don't touch below this line
7+
8+
async function getItemData() {
9+
const response = await fetch('https://api.boot.dev/v1/courses_rest_api/learn-http/items', {
10+
method: 'GET',
11+
mode: 'cors',
12+
headers: {
13+
'X-API-Key': apiKey,
14+
'Content-Type': 'application/json'
15+
}
16+
})
17+
return response.json()
18+
}
19+
20+
function generateKey() {
21+
const characters = 'ABCDEF0123456789'
22+
let result = ''
23+
for (let i = 0; i < 16; i++){
24+
result += characters.charAt(Math.floor(Math.random() * characters.length))
25+
}
26+
return result
27+
}
28+
29+
function logItems(items) {
30+
for (const item of items) {
31+
console.log(item.name)
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const apiKey = generateKey()
2+
const items = await getItemData()
3+
4+
logItems(items)
5+
6+
// don't touch below this line
7+
8+
async function getItemData() {
9+
const response = await fetch('https://api.boot.dev/v1/courses_rest_api/learn-http/items', {
10+
method: 'GET',
11+
mode: 'cors',
12+
headers: {
13+
'X-API-Key': apiKey,
14+
'Content-Type': 'application/json'
15+
}
16+
})
17+
return response.json()
18+
}
19+
20+
function generateKey() {
21+
const characters = 'ABCDEF0123456789'
22+
let result = ''
23+
for (let i = 0; i < 16; i++){
24+
result += characters.charAt(Math.floor(Math.random() * characters.length))
25+
}
26+
return result
27+
}
28+
29+
function logItems(items) {
30+
for (const item of items) {
31+
console.log(item.name)
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Healing Potion
3+
4+
Light Leather
5+
6+
Padded Leather
7+
8+
Haste Potion
9+
10+
Leather Scraps
11+
12+
Copper Ore
13+
14+
Copper Bar
15+
16+
Iron Ore
17+
18+
Iron Bar
19+
20+
Gold Ore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Communicating on the web
2+
3+
Instagram would be pretty terrible if you have to manually copy your photos to your friend's phone when you wanted to share them. Modern applications need to be able to communicate information *between devices* over the internet.
4+
5+
* Gmail doesn't just store your emails in variables on your computer, it stores them on computers in their data centers
6+
* You don't lose your slack messages if you drop your computer in a lake, those messages exist on Slack's [servers](https://en.wikipedia.org/wiki/Web_server)
7+
8+
## How does web communication work?
9+
10+
When two computers communicate with each other, they need to use the same rules. An English speaker can't communicate verbally with a Japanese speaker, similarly, two computers need to speak the same language to communicate.
11+
12+
This "language" that computers use is called a [protocol](https://en.wikipedia.org/wiki/Communication_protocol). The most popular protocol for web communication is [HTTP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview), which stands for Hypertext Transfer Protocol.
13+
14+
## Fantasy Quest
15+
16+
The text-based adventure game *Fantasy Quest* we've been working on in previous courses has come a long way, but we still have a lot to improve.
17+
18+
As it stands, "Fantasy Quest" is a game that's local to our player's devices. If they lose their computer, they have to start over! We want players to have their save data stored on our [servers](https://en.wikipedia.org/wiki/Web_server), and we want our players to be able to interact with each other over the internet. Let's use HTTP to do that!
19+
20+
## Assignment
21+
22+
I wrote the `getItemData()` function for you. It retrieves items from Fantasy Quest's servers via HTTP.
23+
24+
I also wrote a `logItems()` function for you, call it with the `items` variable we are getting back from the `getItemData()` as a parameter.
25+
26+
## Tip
27+
28+
Notice how none of the data that is logged to the console was generated within our code! That's because the data we retrieved is being sent over the internet from our servers via HTTP. Don't worry, we'll explain more about that later.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"question": "Which comes first?",
3+
"answers": [
4+
"HTTP Request",
5+
"HTTP Response"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# HTTP Requests and Responses
2+
3+
At the heart of HTTP is a simple request-response system. The "requesting" computer, also known as the ["client"](https://en.wikipedia.org/wiki/Client_(computing)), asks another computer for some information. That computer, ["the server"](https://en.wikipedia.org/wiki/Server_(computing)) sends back a response with the information that was requested.
4+
5+
![requests and responses HTTP](https://i.imgur.com/ReFw6nN.png)
6+
7+
We'll talk about the specifics of how the "requests" and "responses" are formatted later. For now, just think of it as a simple question-and-answer system.
8+
9+
* Request: "What are the items in the Fantasy Quest game?"
10+
* Response: `A list of the items in the Fantasy Quest game`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"question": "Website data is requested via which internet communication protocol?",
3+
"answers": [
4+
"HTTP",
5+
"UDP",
6+
"RabbitMQ",
7+
"PSQL"
8+
]
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# HTTP powers websites
2+
3+
As we discussed, [HTTP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview), or Hypertext Transfer Protocol, is a [protocol](https://developer.mozilla.org/en-US/docs/Glossary/Protocol) designed to transfer information between computers.
4+
5+
There are other protocols for communicating over the internet, but HTTP is the most popular and is *particularly great for websites and web applications*. Each time you visit a website, your browser is making an HTTP request to that website's server. The server responds with all the text, images, and styling information that your browser needs to render its pretty website!
6+
7+
![website image](https://i.imgur.com/EflKJzq.jpg)
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const apiKey = generateKey()
2+
const itemURL = 'https://api.boot.dev/v1/courses_rest_api/learn-http/items'
3+
4+
const items = await getData()
5+
6+
logItems(items)
7+
8+
// don't touch below this line
9+
10+
async function getData(url) {
11+
const response = await fetch(url, {
12+
method: 'GET',
13+
mode: 'cors',
14+
headers: {
15+
'X-API-Key': apiKey,
16+
'Content-Type': 'application/json'
17+
}
18+
})
19+
return response.json()
20+
}
21+
22+
function generateKey() {
23+
const characters = 'ABCDEF0123456789'
24+
let result = ''
25+
for (let i = 0; i < 16; i++){
26+
result += characters.charAt(Math.floor(Math.random() * characters.length))
27+
}
28+
return result
29+
}
30+
31+
function logItems(items) {
32+
for (const item of items) {
33+
console.log(item.name)
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const apiKey = generateKey()
2+
const itemURL = 'https://api.boot.dev/v1/courses_rest_api/learn-http/items'
3+
4+
const items = await getData(itemURL)
5+
6+
logItems(items)
7+
8+
// don't touch below this line
9+
10+
async function getData(url) {
11+
const response = await fetch(url, {
12+
method: 'GET',
13+
mode: 'cors',
14+
headers: {
15+
'X-API-Key': apiKey,
16+
'Content-Type': 'application/json'
17+
}
18+
})
19+
return response.json()
20+
}
21+
22+
function generateKey() {
23+
const characters = 'ABCDEF0123456789'
24+
let result = ''
25+
for (let i = 0; i < 16; i++){
26+
result += characters.charAt(Math.floor(Math.random() * characters.length))
27+
}
28+
return result
29+
}
30+
31+
function logItems(items) {
32+
for (const item of items) {
33+
console.log(item.name)
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Healing Potion
3+
4+
Light Leather
5+
6+
Padded Leather
7+
8+
Haste Potion
9+
10+
Leather Scraps
11+
12+
Copper Ore
13+
14+
Copper Bar
15+
16+
Iron Ore
17+
18+
Iron Bar
19+
20+
Gold Ore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Hint
2+
3+
Don't use the `itemURL` directly in the call to `fetch`. Pass it in as a parameter to `getData`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# HTTP URLs
2+
3+
A URL, or [Uniform Resource Locator](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL), is essentially the address of another computer, or "server" on the internet. Part of the URL specifies how to reach the server, and part of it tells the server what information we want - but more on that later.
4+
5+
For now, it's important to understand that a URL represents a piece of information on another computer that we want access to. We can get access to it by making a *request*, and reading the *response* that the server replies with.
6+
7+
## Assignment
8+
9+
As we talked about in the last assignment, the `getItemData()` function was requesting a resource from a server to ask for some information about the items in the "Fantasy Quest" game.
10+
11+
I've updated the code to be more flexible. The `getData()` function now takes a URL as a parameter. You'll notice that if you run the code in its current state you get a strange error about "unexpected tokens". That's because we're not getting a proper HTTP response. After all, we aren't making our request to a valid URL. Fix the code so that the `getData` function requests the `itemURL`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"question": "HTTP is the only protocol that uses URLs",
3+
"answers": [
4+
"False",
5+
"True"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Using URLs in HTTP
2+
3+
The `http://` at the beginning of a [website URL](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL) specifies that the `http` protocol will be used for communication.
4+
5+
![http in url](https://i.imgur.com/6jiaXBn.png)
6+
7+
Other communication protocols use URLs as well, (hence "Uniform Resource Locator"). That's why we need to be specific when we're making HTTP requests by prefixing the URL with `http://`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"question": "Does the example code run on a client or a server?",
3+
"answers": [
4+
"Client",
5+
"Server"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Requests and Responses Quiz
2+
3+
![requests and responses HTTP](https://i.imgur.com/ReFw6nN.png)
4+
5+
* A "client" is a computer making an HTTP request
6+
* A "server" is a computer responding to an HTTP request
7+
* A computer can be a client, a server, both, or neither. "Client" and "server" are just words we use to describe what computers are doing within a communication system.
8+
* Clients send requests and receive responses
9+
* Servers receive requests and send responses
10+
11+
## Example code
12+
13+
```js
14+
const itemURL = 'https://api.boot.dev/v1/courses_rest_api/learn-http/items'
15+
16+
const items = await getData(itemURL)
17+
18+
console.log(items)
19+
20+
async function getData(url) {
21+
const response = await fetch(url, {
22+
method: 'GET',
23+
mode: 'cors',
24+
headers: {
25+
'X-API-Key': 'Testing',
26+
'Content-Type': 'application/json'
27+
}
28+
})
29+
return response.json()
30+
}
31+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"question": "The computer responding to requests at the URL 'https://api.boot.dev' is a...",
3+
"answers": [
4+
"Server",
5+
"Client"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Requests and Responses Quiz
2+
3+
![requests and responses HTTP](https://i.imgur.com/ReFw6nN.png)
4+
5+
* A "client" is a computer making an HTTP request
6+
* A "server" is a computer responding to an HTTP request
7+
* A computer can be a client, a server, both, or neither. "Client" and "server" are just words we use to describe what computers are doing within a communication system.
8+
* Clients send requests and receive responses
9+
* Servers receive requests and send responses
10+
11+
## Example code
12+
13+
```js
14+
const itemURL = 'https://api.boot.dev/v1/courses_rest_api/learn-http/items'
15+
16+
const items = await getData(itemURL)
17+
18+
console.log(items)
19+
20+
async function getData(url) {
21+
const response = await fetch(url, {
22+
method: 'GET',
23+
mode: 'cors',
24+
headers: {
25+
'X-API-Key': 'Testing',
26+
'Content-Type': 'application/json'
27+
}
28+
})
29+
return response.json()
30+
}
31+
```

0 commit comments

Comments
 (0)