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.

package main

import (
    "context"
    "fmt"
    "io"
    "log"
    "net/http"
)

func registerHandler(responseWriter http.ResponseWriter, request *http.
Request) {
   if request.Method == "GET" {
        io.WriteString(responseWriter, "This is a get request")
    } else if request.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) 
{

    if request.Method == "GET" {
        io.WriteString(responseWriter, "This is a get request")
    } else if request.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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/52a497c0-0c94-4005-94c1-f0ac3ee170d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to