Rest api without gorilla - PUT call 404


(Gowtham Girithar) #1

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”)
}
}


(George Calianu) #2

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:


(Gowtham Girithar) #3

Hi George,

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


(Johan Dahl) #4

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


(Gowtham Girithar) #5

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


(George Calianu) #6

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.