Code-Alongs are live experiences taught by expert instructors designed to prepare you for concepts found in the sprint challenges. These sessions are your opportunity to work on complex job-ready problems in a live and engaging environment.
Code-Alongs are live classes 50 minutes in length designed to offer deeper insights into learning your core competencies and are offered seven days a week in the morning, afternoon, and evening.
This code-along builds on the fundamental concept of login requests in web development. You'll learn how a simple action like logging into a website involves making a network request, focusing on the POST request method, headers, and payloads.
When you enter credentials on a login page, you're sending a POST request to the server with:
The server processes this request and sends back a response with its own headers and body, which might contain a welcome message or an error code like 401 Unauthorized.
In successful login attempts, the server responds with a token—a string that represents your authentication status. This token is crucial for:
// Login request const loginUser = async (credentials) => { try { const response = await fetch('https://api.example.com/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(credentials) }); if (!response.ok) { throw new Error('Login failed'); } const data = await response.json(); // Store the token for future requests localStorage.setItem('authToken', data.token); return data; } catch (error) { console.error('Error during login:', error); throw error; } };
During this code-along, you'll learn to use powerful tools for inspecting and debugging HTTP traffic:
In this code-along, you'll learn how to use the fetch
API in JavaScript to make more complex HTTP requests like POST, PUT, and DELETE. You'll explore how to customize these requests using the second argument of fetch
to specify the method, body, and headers.
PUT requests are used to update existing resources on a server. The key components include:
// PUT request to update a resource const updateResource = async (id, updatedData) => { try { // Get the auth token from localStorage const token = localStorage.getItem('authToken'); const response = await fetch(`https://api.example.com/resources/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(updatedData) }); if (!response.ok) { throw new Error('Update failed'); } return await response.json(); } catch (error) { console.error('Error updating resource:', error); throw error; } };
This code-along will help you understand how to perform all CRUD (Create, Read, Update, Delete) operations using the Fetch API:
You'll also learn proper error handling techniques for network requests, including: