Once we run the program, the HTTP server will start locally listening on port 8080.
Next, executing a POST request from the command line as follows will add an employee to the list with ID as 3 and return the list of employees as a response:
$ curl -H "Content-Type: application/json" -X POST -d '{"Id":"3", "firstName":"Quux", "lastName":"Corge"}' http://localhost:8080/employee/add
This is shown in the following screenshot:
Let’s understand the change we introduced in this recipe:
- First, we added another route with the name addEmployee that executes the addEmployee handler for every POST request for the URL pattern /employee/add.
- Then, we defined an addEmployee handler, which basically decodes the employee data that comes as part of a POST request using the NewDecoder handler of the built-in encoding/json package of Go, appends it to the initial static array of an employee, and writes it to an HTTP response stream.