Rest api without gorilla - PUT call 404

Hi,

I’m writing the REST API without using the gorilla lib and when I do the PUT request , it throws 404.I always seen examples using the gorilla. without gorilla how can I solve the below problem.
adding to the above, what is the adv of using the gorilla

mux.HandleFunc("/Employee/{id}", handleEmployee)

func handleEmployee(writer http.ResponseWriter, request *http.Request) {
switch request.Method {
case “GET”:
case “POST”:
case “PUT”:
pathValues := request.URL.Query() //return map
empid := pathValues.Get(“id”)
println("the emp id is ", empid)
case “DELETE”:
default:
println(“None of the methods other than above are allowed”)
}
}

2 Likes

Take a look over the example below to see how the things works.

var r = mux.NewRouter()

func getEmployee(writer http.ResponseWriter, request *http.Request) {
	// use this to read gorilla mux path variables
	vars := mux.Vars(r)
	id := vars["id"]
	// use this to read from query string
	query := r.URL.Query()
	param1 := query.Get("param1")
	...
}

func main() {
	...
	r.HandleFunc("/employees/{id}", getEmployee).Methods("GET")
	r.HandleFunc("/employees", createEmployee).Methods("POST")
	...
    if err := http.ListenAndServe(":8080", r); err != nil {
         log.Fatalln(err)
    }
}

And some useful resources:

2 Likes

Hi George,

Thank you for your reply.
I want to do without gorilla dependency and with NewServeMux.

2 Likes

Hi. You must write something to the writer I think. Please try this

2 Likes

Hi,
post method is working without writing something to the writer
Every example uses the gorilla mux newrouter way.i want to use golang package http with newservemux

2 Likes

Until a well argumented reason stop you to use dependencies you can use with trust gorilla/mux because it’s pretty hard to handle routes only with standard library.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.