Hi All,
Added customerId field in the data model. Still the same 404 error. Not returning the customer ID.
Can anyone please help? Thank you so much in advance.
Code:
package main
import (
“encoding/json”
“fmt”
“log”
“net/http”
)
type Customer struct {
FullName string
City string
ZipCode string
CustomerId int
}
func main() {
router := http.NewServeMux()
router.HandleFunc(“/greet”, myHandler)
router.HandleFunc(“/customers”, customers)
router.HandleFunc(“/customers/{CustomerId}”, getCustomer)
log.Fatal(http.ListenAndServe(“localhost:8080”, router))
}
func myHandler(w http.ResponseWriter, c *http.Request) {
fmt.Fprint(w, “Hello World, Welcome to the world of GOlang REST API”)
}
func getCustomer(w http.ResponseWriter, c *http.Request) {
vars := mux.Vars(c)
fmt.Fprint(w, vars[“CustomerId”])
}
func customers(w http.ResponseWriter, c *http.Request) {
curt := []Customer{
{FullName: “Alice”, City:“Canada”, ZipCode: “100090”, CustomerId: 12345},
{FullName: “Bob”, City: “Japan”, ZipCode: “100095”, CustomerId:90000},
{FullName: “Marshall”, City:“Australia”, ZipCode: “100096”, CustomerId: 12345},
}
w.Header().Add(“Content-Type”,“application/json”)
json.NewEncoder(w).Encode(curt)
}
Output: