In this tutorial, we will cover the fundamentals of HTTP networking, fetch API, and using REST APIs in JavaScript. By the end of this course, you should have a good understanding of how to make network requests in JavaScript, handle responses, and work with REST APIs.
Section 1: Introduction to HTTP Networking
Before we dive into the details of fetch and REST APIs, let’s first understand the basics of HTTP networking. HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the World Wide Web. It is a request-response protocol, where a client sends a request to a server, and the server responds with the requested data.
HTTP has several methods (also known as verbs) that define the actions that can be performed on a resource. Some of the common HTTP methods include GET (to retrieve data), POST (to submit data), PUT (to update data), DELETE (to delete data), etc.
Section 2: Understanding Fetch API
The fetch API is a modern way to make network requests in JavaScript. It is built into modern browsers and provides a simple and efficient way to fetch resources from a server. Here’s a basic example of using the fetch API to make a GET request:
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
In this example, we use the fetch function to make a GET request to a URL. We then use the then
method to handle the response and convert it to JSON format. Finally, we use another then
method to log the data to the console.
Section 3: Working with REST APIs
REST (Representational State Transfer) is a popular architectural style for building APIs. REST APIs use standard HTTP methods and status codes to interact with resources. In REST, each resource is identified by a unique URL (also known as an endpoint), and different HTTP methods are used to perform different actions on that resource.
Let’s see an example of how to make a POST request to a REST API using the fetch API:
const data = {
name: 'John Doe',
email: 'john@example.com'
};
fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
In this example, we send a POST request to the /users
endpoint with some JSON data. We set the method
option to ‘POST’, specify the Content-Type
header as ‘application/json’, and stringify the data using JSON.stringify
before sending it.
Section 4: Error Handling and Asynchronous Code
When working with network requests, it is important to handle errors properly and work with asynchronous code. In the examples above, we used the catch
method to catch any errors that occur during the network request.
You can also use async/await
syntax to make your code more readable and easier to understand. Here’s an example of using async/await with fetch:
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
} catch (error) {
console.log(error);
}
}
fetchData();
In this example, we define an async function fetchData
that uses await
to make the network request and handle the response. We then call the function fetchData
to fetch the data asynchronously.
Section 5: Conclusion
In this course, we covered the basics of HTTP networking, fetch API, and working with REST APIs in JavaScript. By understanding these concepts and practicing with examples, you should have a good foundation for working with network requests in your JavaScript applications.
If you have any questions or need further clarification on any topic covered in this course, feel free to ask in the comments section. Happy coding!
I hope the video is helpful! It was a ton of fun to record
Thank you
should i learn js first ?
43:50 wow wow,, how do you set up seo metrics plugin?
Excellent! I'm a professional Youtube uploader myself and I'm quite aware how difficult it is to make a single video. Bravo! Keep up the good work.
interesting course, i did learn some cool stuff <3
3:03:28
big respect Lane, brilliant courses
At 1:19:39, it should print second.
I would learn anything from this guy. Very interesting to watch. Thank you
funny humour 🙂 what the heck does HTTP have to do with Instagram image downloads, lol.
Thank you very much
36:00 Here you're calling Cloudflair directly to get the domain's name's IP address. Is the browser doing the same?
Edit… From 44:00, your computer where the browser is running calls one of ICANN's root nameservers to resolve the domain name into an IP address. These root nameservers's IP addresses are built into each computer's network configuration.
Can this be used to extract email ?
Is this tutorial updated for / still applicable with HTTP/3 w QUIC on UDP
Thank you very much for this course! Especially for the prononciation. For not native speaker like me this is important.
9:29 passing `items` in logItems throws me an error like this. where does problem come from?
const items = await getItemData()
^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules
I admit, that for self-taught frontend developer with year+ of experience, it was great until the project actually started to become too complicated. I think it wasn't actually teaching the http, but rather being "look what you can achieve", that I did not understand after like 4:45:00. The rest was fine, but it wasn't necessary for http learning since there was not much http to experience with.
For those who don't know he copying the code from another screen from his setup for the video , it's copyied logic content like this forces me to change the video
3:51:28