Lesson 2

Diving into HTTP and REST: Making GET Requests and Displaying API Data

Understanding HTTP Protocols

HTTP, or Hypertext Transfer Protocol, is the protocol that allows us to send and receive data over the web. When we enter a URL into our web browser or when we make an API request, we're sending an HTTP request to a server.

An HTTP request includes a "method", which tells the server what kind of operation we want to perform. The most common methods are "GET" (retrieve data), "POST" (send data), "PUT" (update data), and "DELETE" (remove data).

We then receive an HTTP response from the server, which contains a status code. This code tells us if the request was successful or if an error occurred. Common status codes you might see are 200 (OK), 404 (Not Found), or 500 (Internal Server Error).

In addition to the status code, the server returns data to us. For web pages, this is usually an HTML document that your browser renders into visible webpage. For APIs, it is often a JSON or XML document with data.

For instance, if we make a request to https://www.google.com, the server handles this request, comes back with a 200 status code (which means everything went OK), and returns the HTML file of Google's homepage. Our browser then takes this HTML file and displays the page accordingly.

Remember that these requests and responses form the basis of web communication, and APIs utilize these HTTP protocols to facilitate the interaction between different software applications.

Exploring API Types and RESTful APIs

APIs (Application Programming Interfaces) span various types, with the most important ones being SOAP, REST, and GraphQL.

REST (Representational State Transfer) defines an architectural style for APIs. APIs using this style are referred to as RESTful APIs. They've gained widespread use because they are simple, stateless, and compatible with HTTP. Any 'memory' during communication is transmitted within the request and response, making them efficient and highly scalable — qualities vital for modern web applications.

Introduction to GET Requests

Within the realm of HTTP, GET functions as a method used to retrieve or get data. When you visit a webpage, you are essentially making a GET request to the server, asking it for an HTML file.

In JavaScript, we can invoke a GET request using the Fetch API, as exemplified below:

1fetch('https://api-regional.codesignalcontent.com/posting-application-2/posts/') // sends a GET request to the API URL 2 .then(response => response.json()) // when the server responds, we turn the response into a JavaScript object 3 .then(data => console.log(data)); // and then we log this object to the console

This handy function empowers us to fetch data from an API and display it in our console.

Displaying API Data in the UI

When we fetch data from an API, we often want to display this data in our web page. JavaScript gives us many ways to manipulate the document and show our data.

With JavaScript, we can create new elements, update the content of existing elements, add styles, and much more.

Let's suppose our API call returns a list of user data, and we want to display each user's name. First, we fetch the data:

1fetch('https://api.example.com/users') 2 .then(response => response.json()) 3 .then(data => { 4 // Here, 'data' is an array of user objects returned from the API 5 console.log('Data parsed from JSON:', data); 6 displayUsers(data); 7});

And next, here's the displayUsers function. This function creates new elements for each user's data, and adds them to our webpage:

1function displayUsers(users) { 2 const userList = document.getElementById('userList'); 3 4 for(let i = 0; i < users.length; i++) { 5 const listItem = document.createElement('li'); 6 listItem.textContent = users[i].name; 7 userList.appendChild(listItem); 8 } 9}

In this function, we use a for loop to iterate through the users array. For each user at the index i, we create a new li list item element, set its text to the user's name, and then add it to the userList ul list element on our webpage. Once this function has run, each user's name should be displayed on our webpage as an individual list item!

Importance of API Documentation

When working with APIs, one of the quintessential tools is the API documentation. Essentially, it is a manual that guides you on how to deploy the API. It highlights the supported endpoints, request methods (GET, POST, etc.), required parameters, and the responses that are returned.

Is is therefore critical to become comfortable with API documentation. It's often the first place to look when educating oneself on how to work with a new API.

Lesson Summary

By this point, you've acquainted yourself with the HTTP protocol, types of APIs like RESTful APIs, the process of making GET requests using JavaScript, and displaying API data on your webpage. Also, you've realized the significance of API documentation for effectively utilizing APIs.

Coming up next, exercises await you to consolidate these crucial insights. You'll be learning practical aspects of API usage, equipping you for the web development adventures to come. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.