PUT and DELETE requests let your app update or remove data on a server. They help keep your app's data fresh and accurate.
PUT and DELETE requests in Angular
this.httpClient.put(url, body, options) this.httpClient.delete(url, options)
PUT sends the full updated data to replace existing data.
DELETE removes data identified by the URL.
this.httpClient.put('https://api.example.com/users/1', {name: 'Anna', age: 30})
this.httpClient.delete('https://api.example.com/users/1')
This Angular component has two buttons. Clicking 'Update User' sends a PUT request to change user data. Clicking 'Delete User' sends a DELETE request to remove the user. The message below shows success or failure.
import { Component } from '@angular/core'; import { HttpClient, HttpClientModule } from '@angular/common/http'; @Component({ selector: 'app-user-manager', template: ` <button (click)="updateUser()">Update User</button> <button (click)="deleteUser()">Delete User</button> <p>{{message}}</p> `, standalone: true, imports: [HttpClientModule] }) export class UserManagerComponent { message = ''; constructor(private httpClient: HttpClient) {} updateUser() { const url = 'https://jsonplaceholder.typicode.com/users/1'; const updatedData = { name: 'Anna', username: 'anna123' }; this.httpClient.put(url, updatedData).subscribe({ next: () => this.message = 'User updated successfully!', error: () => this.message = 'Failed to update user.' }); } deleteUser() { const url = 'https://jsonplaceholder.typicode.com/users/1'; this.httpClient.delete(url).subscribe({ next: () => this.message = 'User deleted successfully!', error: () => this.message = 'Failed to delete user.' }); } }
Always handle errors to inform users if the request fails.
PUT replaces the entire resource, so send all required fields.
DELETE requests usually don't need a body, just the URL identifying the item.
PUT updates existing data by sending the full new version.
DELETE removes data identified by the URL.
Use Angular's HttpClient to easily send these requests and handle responses.
Practice
PUT request in Angular's HttpClient?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 CQuick Check:
PUT updates full resource = A [OK]
- Confusing PUT with DELETE
- Thinking PUT partially updates data
- Mixing GET with PUT
Solution
Step 1: Identify the method for DELETE
Angular's HttpClient usesdelete()to send DELETE requests.Step 2: Check syntax correctness
The method callthis.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 AQuick Check:
DELETE method uses http.delete() [OK]
- Using http.put() instead of http.delete()
- Forgetting to subscribe to the observable
- Using http.post() for deleting
const updatedItem = { id: 5, name: 'New Name' };
this.http.put('api/items/5', updatedItem).subscribe(response => {
console.log(response);
});Solution
Step 1: Understand PUT request behavior
PUT sends the full updated data and usually returns the updated resource from the server.Step 2: Analyze the subscription callback
Theresponselogged is the server's reply, typically the updated item object.Final Answer:
The updated item object returned from the server -> Option AQuick Check:
PUT returns updated data = A [OK]
- Assuming PUT returns no data
- Confusing PUT with DELETE response
- Expecting original item instead of updated
this.http.delete('api/items').subscribe(response => {
console.log('Item deleted');
});Assuming the API requires an item ID to delete a specific item.
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 BQuick Check:
DELETE URL needs item ID = C [OK]
- Sending DELETE without specifying resource ID
- Thinking DELETE needs a request body
- Ignoring subscribe callback
// 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');
});
});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 DQuick Check:
Update then delete sequentially = B [OK]
- Deleting before updating causing data mismatch
- Sending requests simultaneously without order
- Trying to delete picture with PUT request
Ready to prove it?
Pass the test and this pattern moves unknown
