This lesson demonstrates creating a route for handling POST requests to insert new products into a database.
In this lesson, we will demonstrate how to create a new route that handles POST requests for inserting a new product into the database. This guide covers updating routes, implementing database methods for product insertion, creating the HTTP handler for the POST request, and testing the new endpoint.
First, update the routes in your application to include a new route for creating products. In addition to the existing routes for fetching products, we’ll add a route that handles POST requests for inserting new products.
Next, implement the method that inserts a product into the database. This method constructs an INSERT query using the product’s name, quantity, and price. After executing the query, it retrieves the last inserted ID and assigns it to the product struct.
Now, create the HTTP handler function that will process the POST requests. This handler is responsible for decoding the JSON payload sent by the client. If the payload cannot be decoded properly, the function returns an error response with the HTTP status “Bad Request”.
Copy
Ask AI
func (app *App) createProduct(w http.ResponseWriter, r *http.Request) { var p product // Decode the JSON request payload into a product object err := json.NewDecoder(r.Body).Decode(&p) if err != nil { sendError(w, http.StatusBadRequest, "Invalid request payload") return } // Insert the product into the database err = p.createProduct(app.DB) if err != nil { sendError(w, http.StatusInternalServerError, err.Error()) return } sendResponse(w, http.StatusOK, p)}
Ensure that your routes are properly updated (as described in the previous section) so that the POST requests to /product are routed to the createProduct function.
After building and running your application, test the new endpoint by sending a sample JSON payload. For instance, you can use a tool like Postman or curl to send a POST request to the /product endpoint with the following JSON:
Copy
Ask AI
{ "name": "Pens", "price": 10, "quantity": 100}
If the insertion is successful, you will see a confirmation. However, if there is a mismatch between the number of columns and values, you might receive an error such as:
Copy
Ask AI
{ "error": "Error 1136: Column count doesn't match value count at row 1"}
This error indicates that the number of values provided in the INSERT statement does not match the columns in the products table. Verify your INSERT query to ensure the columns and values align correctly.
If your newly added product is visible in the output, your POST endpoint is functioning correctly.This lesson covered updating routes, implementing model methods for data insertion, decoding JSON from POST requests, and testing the endpoint. Happy coding!