Creating your first HTTP POST method
Whenever we have to send data to the server either through an asynchronous call or through an HTML form, then we go with the HTTP POST method implementation, which we will cover in this recipe.
How to do it…
- Install the
github.com/gorilla/muxpackage using thego getcommand, as follows:
$ go get github.com/gorilla/mux- Create
http-rest-post.gowhere we will define an additional route that supports the HTTPPOSTmethod and a handler that adds an employee to the initial static array of employees and writes the updated list to an HTTP response stream, as follows:
package main
import
(
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
const
(
CONN_HOST = "localhost"
CONN_PORT = "8080"
)
type Route struct
{
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
type Routes []Route
var routes = Routes
{
Route
{
"getEmployees",
"GET",
"/employees",
getEmployees,
},
Route
{
"addEmployee...