Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3655

Go 1.22 router calls the wrong method [closed]

$
0
0

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 controllerimport ("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 mainimport ("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.


Viewing all articles
Browse latest Browse all 3655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>