I believe this is documented here: https://golang.org/pkg/net/http/#ServeMux

On 07/06/2019 07:46, Mayank Gupta wrote:
Hi All, I wanted to make sure before filing an issue on Github. Here's the code using net/http package to create a restful web API. If I hit any route using Postman app, no matter the type of request (POST, PUT, DELETE, etc.) it always hits the API as a GET request.
|
packagemain import("context""fmt""io""log""net/http")func registerHandler(responseWriter http.ResponseWriter,request *http.Request){ifrequest.Method=="GET"{        io.WriteString(responseWriter,"This is a get request")}elseifrequest.Method=="POST"{        io.WriteString(responseWriter,"This is a post request")}else{        io.WriteString(responseWriter,"This is not a valid request request")}}func statsHandler(responseWriter http.ResponseWriter,request *http.Request){ifrequest.Method=="GET"{        io.WriteString(responseWriter,"This is a get request")}elseifrequest.Method=="POST"{        io.WriteString(responseWriter,"This is a post request")}else{        io.WriteString(responseWriter,"This is not a valid request request")}}func main(){    mux :=http.NewServeMux()    mux.HandleFunc("/register/",registerHandler)    mux.HandleFunc("/stats/",statsHandler)    log.Fatal(http.ListenAndServe(":5000",mux))}
|
If I hit 'http://localhost:5000/stats' using POST, it'll always hit the API as a GET request.

Changing main() function to:
|
func main(){

    mux :=http.NewServeMux()
    mux.HandleFunc("/register",registerHandler)
    mux.HandleFunc("/stats",statsHandler)

    log.Fatal(http.ListenAndServe(":5000",mux))

}
|
Works like a charm, the only change is I removed '/' from the routes at end.

I believe it's a bug. Please let me know if what I believe is wrong.

Thanks,
Mayank Gupta

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com <mailto:golang-nuts+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/52a497c0-0c94-4005-94c1-f0ac3ee170d6%40googlegroups.com <https://groups.google.com/d/msgid/golang-nuts/52a497c0-0c94-4005-94c1-f0ac3ee170d6%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ab7c3e3c-5cac-372a-3fa2-394261a70429%40multiplay.co.uk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to