POST, PUT and DELETE
Change data on the server.
Reading isn't enough. Our shop also needs to add new books, change them, and remove them, on the server.
- Add
- Change
- Remove
book-api.ts
Add posts a new book, without an id, because the server gives it one. Put sends the changes, to one book's address. And remove sends a delete request. Each one returns an observable.
import { Service, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
export interface Book { id: number; title: string; author: string; price: number; }
@Service()
export class BookApi {
private http = inject(HttpClient);
private url = '/api/books';
all() { return this.http.get<Book[]>(this.url); }
one(id: number) { return this.http.get<Book>(`${this.url}/${id}`); }
add(b: Omit<Book, 'id'>) { return this.http.post<Book>(this.url, b); }
put(id: number, b: Partial<Book>) { return this.http.put<Book>(`${this.url}/${id}`, b); }
remove(id: number) { return this.http.delete(`${this.url}/${id}`); }
}Adding a book
Load gets every book, into the signal. Add posts Persuasion, and loads the list again, when the server replies. We click add. Run it. The server answered the post with two oh one, created. Persuasion now has id five, given by the server.
import { Component, inject, signal } from '@angular/core';
import { BookApi, Book } from './book-api';
@Component({
selector: 'app-root',
template: `
<button (click)="add()">Add Persuasion</button>
@for (b of books(); track b.id) { <p>{{ b.id }}. {{ b.title }}</p> }
`,
})
export class App {
private api = inject(BookApi);
books = signal<Book[]>([]);
constructor() { this.load(); }
load() { this.api.all().subscribe(list => this.books.set(list)); }
add() {
const book = { title: 'Persuasion', author: 'Jane Austen', price: 7.49 };
this.api.add(book).subscribe(() => this.load());
}
}API GET /api/books → 200 API POST /api/books → 201 API GET /api/books → 200
Also uses book-api.ts
import { Service, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
export interface Book { id: number; title: string; author: string; price: number; }
@Service()
export class BookApi {
private http = inject(HttpClient);
private url = '/api/books';
all() { return this.http.get<Book[]>(this.url); }
one(id: number) { return this.http.get<Book>(`${this.url}/${id}`); }
add(b: Omit<Book, 'id'>) { return this.http.post<Book>(this.url, b); }
put(id: number, b: Partial<Book>) { return this.http.put<Book>(`${this.url}/${id}`, b); }
remove(id: number) { return this.http.delete(`${this.url}/${id}`); }
}Also uses actions.json
[{"click": "button"}, {"wait": 900}]Also uses _apilog
1Updating and deleting
Rename sends a put request, with a new title, for book one. Remove deletes book two. Both reload the list afterwards. We click both buttons. Run it. Put returned two hundred, and delete returned two oh four, no content. Book one is renamed, and book two is gone.
import { Component, inject, signal } from '@angular/core';
import { BookApi, Book } from './book-api';
@Component({
selector: 'app-root',
template: `
<button (click)="rename()">Rename book 1</button>
<button (click)="remove()">Delete book 2</button>
@for (b of books(); track b.id) { <p>{{ b.id }}. {{ b.title }}</p> }
`,
})
export class App {
private api = inject(BookApi);
books = signal<Book[]>([]);
constructor() { this.load(); }
load() { this.api.all().subscribe(list => this.books.set(list)); }
rename() { this.api.put(1, { title: 'Dune Messiah' }).subscribe(() => this.load()); }
remove() { this.api.remove(2).subscribe(() => this.load()); }
}API GET /api/books → 200 API PUT /api/books/1 → 200 API GET /api/books → 200 API DELETE /api/books/2 → 204 API GET /api/books → 200
Also uses book-api.ts
import { Service, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
export interface Book { id: number; title: string; author: string; price: number; }
@Service()
export class BookApi {
private http = inject(HttpClient);
private url = '/api/books';
all() { return this.http.get<Book[]>(this.url); }
one(id: number) { return this.http.get<Book>(`${this.url}/${id}`); }
add(b: Omit<Book, 'id'>) { return this.http.post<Book>(this.url, b); }
put(id: number, b: Partial<Book>) { return this.http.put<Book>(`${this.url}/${id}`, b); }
remove(id: number) { return this.http.delete(`${this.url}/${id}`); }
}Also uses actions.json
[{"click": "button"}, {"wait": 900}, {"click": "button:nth-of-type(2)"}, {"wait": 900}]Also uses _apilog
1Summary
Post creates, put updates, and delete removes. Subscribe to send each one, then reload the data. Next, loading, and errors.
Key takeaways- post()
- put()
- delete()