📖 Scenario: You are building a simple Angular app to manage a list of books. You want to update book details and delete books from the list by sending requests to a server.
🎯 Goal: Build an Angular component that can send PUT requests to update a book's title and DELETE requests to remove a book from the server.
📋 What You'll Learn
Create a list of books with id and title
Add a variable to hold the API base URL
Write a method to send a PUT request to update a book's title
Write a method to send a DELETE request to remove a book by id
💡 Why This Matters
🌍 Real World
Managing data on a server is common in web apps. PUT and DELETE requests let you update and remove data remotely.
💼 Career
Understanding how to use Angular's HttpClient for PUT and DELETE requests is essential for frontend developers working with REST APIs.
Progress0 / 4 steps
1
Create the initial books list
Create a variable called books in the component class. It should be an array of objects with these exact entries: { id: 1, title: 'Angular Basics' }, { id: 2, title: 'TypeScript Guide' }, and { id: 3, title: 'RxJS in Depth' }.
Angular
Hint
Use an array of objects with id and title properties exactly as shown.
2
Add the API base URL
Add a string variable called apiUrl in the component class and set it to 'https://api.example.com/books'.
Angular
Hint
Just add a string variable with the exact URL given.
3
Write the method to update a book with PUT
Add a method called updateBook that takes a book object as a parameter. Inside, use Angular's HttpClient to send a PUT request to `${this.apiUrl}/${book.id}` with the book object as the body. Assume HttpClient is injected as http. The method should return the observable from http.put.
Angular
Hint
Use template strings for the URL and return the observable from http.put.
4
Write the method to delete a book with DELETE
Add a method called deleteBook that takes a bookId number as a parameter. Inside, use Angular's HttpClient to send a DELETE request to `${this.apiUrl}/${bookId}`. Assume HttpClient is injected as http. The method should return the observable from http.delete.
Angular
Hint
Use template strings for the URL and return the observable from http.delete.
Practice
(1/5)
1. What is the main purpose of a PUT request in Angular's HttpClient?
easy
A. To retrieve data from the server
B. To delete a resource from the server
C. To update an existing resource by sending the full new data
D. To partially update a resource
Solution
Step 1: Understand HTTP methods
PUT is used to update or replace an existing resource completely.
Step 2: Compare with other methods
DELETE removes data, GET retrieves data, PATCH partially updates data.
Final Answer:
To update an existing resource by sending the full new data -> Option C
Quick Check:
PUT updates full resource = A [OK]
Hint: PUT replaces full data, DELETE removes resource [OK]
Common Mistakes:
Confusing PUT with DELETE
Thinking PUT partially updates data
Mixing GET with PUT
2. Which of the following is the correct syntax to send a DELETE request using Angular's HttpClient?
easy
A. this.http.delete('api/items/1').subscribe();
B. this.http.put('api/items/1').subscribe();
C. this.http.get('api/items/1').subscribe();
D. this.http.post('api/items/1').subscribe();
Solution
Step 1: Identify the method for DELETE
Angular's HttpClient uses delete() to send DELETE requests.
Step 2: Check syntax correctness
The method call this.http.delete('api/items/1').subscribe(); correctly sends a DELETE request and subscribes to the response.
Final Answer:
this.http.delete('api/items/1').subscribe(); -> Option A
Quick Check:
DELETE method uses http.delete() [OK]
Hint: Use http.delete() for DELETE requests [OK]
Common Mistakes:
Using http.put() instead of http.delete()
Forgetting to subscribe to the observable
Using http.post() for deleting
3. Given this Angular code snippet, what will be logged after the PUT request completes successfully?
Assuming the API requires an item ID to delete a specific item.
medium
A. DELETE requests cannot be sent without a request body
B. The URL is missing the item ID to specify which item to delete
C. The subscribe method is missing a parameter
D. The console.log statement is incorrect
Solution
Step 1: Check the URL for DELETE request
The URL 'api/items' does not specify which item to delete; an ID is needed.
Step 2: Understand API requirements
APIs usually require an ID in the URL like 'api/items/1' to delete a specific item.
Final Answer:
The URL is missing the item ID to specify which item to delete -> Option B
Quick Check:
DELETE URL needs item ID = C [OK]
Hint: DELETE URL must include resource ID to delete [OK]
Common Mistakes:
Sending DELETE without specifying resource ID
Thinking DELETE needs a request body
Ignoring subscribe callback
5. You want to update a user's profile and then delete their old profile picture in Angular. Which sequence of HttpClient calls correctly applies PUT and DELETE requests?
// Update user profile
this.http.put('api/users/10', updatedUser).subscribe(() => {
// Delete old profile picture
this.http.delete('api/pictures/oldPicId').subscribe(() => {
console.log('Profile updated and old picture deleted');
});
});
hard
A. DELETE request first to delete picture, then PUT request inside DELETE's subscribe to update profile
B. Use only PUT request to update profile and delete picture together
C. Send PUT and DELETE requests simultaneously without waiting for response
D. PUT request first to update profile, then DELETE request inside PUT's subscribe to delete picture
Solution
Step 1: Understand request order
First update the user profile with PUT, then delete the old picture after success.
Step 2: Analyze code structure
DELETE request is inside PUT's subscribe callback, ensuring sequential execution.
Step 3: Evaluate other options
Deleting first may remove picture before profile update; simultaneous calls lack order; PUT cannot delete picture.
Final Answer:
PUT request first to update profile, then DELETE request inside PUT's subscribe to delete picture -> Option D
Quick Check:
Update then delete sequentially = B [OK]
Hint: Chain DELETE inside PUT subscribe for correct order [OK]
Common Mistakes:
Deleting before updating causing data mismatch
Sending requests simultaneously without order
Trying to delete picture with PUT request
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.