POST /reviews

This is a post method that lets you use a HTML form or equivalent to submit a new review

Endpoint URL

http://127.0.0.1:8080/api/reviews

JSON body parameters

Name
Type
Description

rating

float

A float value rounded to 1 decimal place that rates the book, out of 5 stars

id

integer

The unique id of the review

bookId

integer

A unique identifier, all reviews about the same book will have the same bookId, and can be differentiated using the author property

title

string

The title of the book that is being reviewed

text

string

The review text that the user has written

reviewer

string

The name of the user who posted this review

Example Code

const reviewForm = document.getElementById("add-review-form"); // Example form

reviewForm.onsubmit = async (e) => {
    e.preventDefault();
    const data = new FormData(e.target);
    const review = Object.fromEntries(data.entries());
    try {
        const response = await fetch("/api/reviews", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(review),
        });

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        } else {
            console.log("Review added successfully");
            reviewForm.reset();
        }
    } catch (error) {
        console.error("Error adding new review:", error);
    }
};

Example response

Response fields

Name
Type
Description

message

string

A message to confirm or post an error, status of the POST request

Last updated