0
0
Angularframework~20 mins

PUT and DELETE requests in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
REST API Mastery in Angular
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the PUT request succeeds?

Consider this Angular component that updates a user profile using a PUT request. What will be the value of updateStatus after a successful response?

Angular
import { Component, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-user-update',
  template: `<button (click)="updateUser()">Update User</button><p>{{updateStatus}}</p>`,
  standalone: true
})
export class UserUpdateComponent {
  private http = inject(HttpClient);
  updateStatus = '';

  updateUser() {
    this.http.put('/api/users/1', { name: 'Alice' }).subscribe({
      next: () => this.updateStatus = 'Update successful',
      error: () => this.updateStatus = 'Update failed'
    });
  }
}
AAn error will be thrown because PUT requests cannot be made this way.
BThe text 'Update successful' will be displayed.
CNothing will change because the PUT request does not update the status.
DThe text 'Update failed' will be displayed.
Attempts:
2 left
💡 Hint

Think about what happens when the next callback runs in the subscription.

📝 Syntax
intermediate
1:30remaining
Which option correctly sends a DELETE request with Angular HttpClient?

Choose the correct syntax to send a DELETE request to /api/items/42 using Angular's HttpClient.

Athis.http.deleteRequest('/api/items/42').subscribe();
Bthis.http.remove('/api/items/42').subscribe();
Cthis.http.del('/api/items/42').subscribe();
Dthis.http.delete('/api/items/42').subscribe();
Attempts:
2 left
💡 Hint

Check the official Angular HttpClient method names for DELETE requests.

state_output
advanced
2:30remaining
What is the value of items after deleting an item?

Given this Angular component code, what will be the value of items after deleteItem(2) is called and the DELETE request succeeds?

Angular
import { Component, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-item-list',
  template: `<ul><li *ngFor="let item of items">{{item.name}}</li></ul>`,
  standalone: true
})
export class ItemListComponent {
  private http = inject(HttpClient);
  items = [
    { id: 1, name: 'Pen' },
    { id: 2, name: 'Notebook' },
    { id: 3, name: 'Eraser' }
  ];

  deleteItem(id: number) {
    this.http.delete(`/api/items/${id}`).subscribe(() => {
      this.items = this.items.filter(item => item.id !== id);
    });
  }
}
A[{ id: 1, name: 'Pen' }, { id: 3, name: 'Eraser' }]
B[{ id: 1, name: 'Pen' }, { id: 2, name: 'Notebook' }, { id: 3, name: 'Eraser' }]
C[]
D[{ id: 2, name: 'Notebook' }]
Attempts:
2 left
💡 Hint

Look at how the items array is updated inside the subscribe callback.

🔧 Debug
advanced
2:00remaining
Why does this PUT request not update the server?

Examine this Angular service method. The PUT request does not update the server as expected. What is the most likely cause?

Angular
updateUser(user: any) {
  this.http.put('/api/users/' + user.id, user);
}
AThe HttpClient does not support PUT requests without a body.
BThe URL is incorrect because it should use backticks for string interpolation.
CThe PUT request is not subscribed to, so it never executes.
DThe user object is missing required headers for PUT requests.
Attempts:
2 left
💡 Hint

Remember how Angular HttpClient observables work when not subscribed.

🧠 Conceptual
expert
1:30remaining
What is the main difference between PUT and DELETE requests in REST APIs?

Choose the best explanation of how PUT and DELETE requests differ in RESTful web services.

APUT replaces or updates a resource, while DELETE removes the resource entirely.
BPUT removes a resource, while DELETE updates the resource partially.
CPUT creates a new resource, while DELETE only reads resource data.
DPUT and DELETE both update resources but differ in request headers.
Attempts:
2 left
💡 Hint

Think about the purpose of each HTTP method in REST.