I am trying to use the new go net/http feature of specifying the Method inside the handlefunc of my Servemux. However when I try to call the POST endpoint it executes the function on my GET endpoint.
package controller
import (
"gofernv2/service"
"net/http"
)
func SetupUserRouter() *http.ServeMux {
userRouter := http.NewServeMux()
userRouter.HandleFunc("GET /{$}", service.GetAllUsers)
userRouter.HandleFunc("POST /{$}", service.PostNewUser)
return userRouter
}
here
This is my user.go controller.
package main
import (
"fmt"
"gofernv2/controller"
"gofernv2/database"
"net/http"
)
func main() {
database.InitDB()
userRouter := controller.SetupUserRouter()
api := http.NewServeMux()
api.Handle("/api/user/", http.StripPrefix("/api/user", userRouter))
server := http.Server{
Addr: ":8900",
Handler: api,
}
fmt.Printf("Server running on port 8900")
server.ListenAndServe()
}
This is my main.go file.
I send a Post Request to “/api/user” but it executes the GetAllUsers Function instead.
New contributor
caesartim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.