The Books resource allows you to manage book records in your application. Each book resource represents a book entity, which includes details such as the title, author, genre, and publication year.
The following fields are available for each book resource:
The Books resource exposes various endpoints to manage book resources. Below are the available endpoints and their HTTP methods:
Method | Endpoint | Description |
---|---|---|
GET | /api/books | Fetch all books. |
GET | /api/books/{id} | Fetch a specific book by ID. |
POST | /api/books | Create a new book. |
PUT | /api/books/{id} | Update a book's information. |
DELETE | /api/books/{id} | Delete a book by ID. |
API requests may include parameters that influence the response. Parameters are categorized as path parameters, query parameters, or request bodies.
For creating or updating a book, the following fields are used in the request body:
Name | Type | Required | Description |
---|---|---|---|
title | string | Yes | The title of the book. |
author | string | Yes | The author of the book. |
genre | string | No | The genre of the book (e.g., Fiction, Non-fiction). |
publicationYear | integer | No | The year the book was published. |
Below are examples for each type of request to the Books resource.
fetch('/api/books')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/books/1')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/books', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: 'The Great Gatsby',
author: 'F. Scott Fitzgerald',
genre: 'Fiction',
publicationYear: 1925
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
These examples illustrate the expected JSON responses for each endpoint.
[
{
"id": 1,
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"genre": "Fiction",
"publicationYear": 1925
},
{
"id": 2,
"title": "1984",
"author": "George Orwell",
"genre": "Dystopian",
"publicationYear": 1949
}
]
{
"id": 1,
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"genre": "Fiction",
"publicationYear": 1925
}
{
"id": 3,
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"genre": "Fiction",
"publicationYear": 1960
}