Skip to content

Instantly share code, notes, and snippets.

@jaybill
Created April 25, 2012 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaybill/2493596 to your computer and use it in GitHub Desktop.
Save jaybill/2493596 to your computer and use it in GitHub Desktop.
Trailing slash forces GET method on request?
package main
import(
"net/http"
"log"
"fmt"
)
func route(pattern string, fn func(*http.Request) string){
http.HandleFunc(pattern,makeHandler(fn))
}
func makeHandler(fn func(*http.Request) (string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("Request method: %q",r.Method) // Method is always get, regardless of what request actually was
h := fn(r)
fmt.Fprintf(w,"Handler said: %q",h)
}
}
func helloHandler(r *http.Request) (view string){
view = "Hello, World!"
return
}
func main(){
route("/hello/",helloHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
<form action="http://localhost:8080/hello" method="post">
<input type="hidden" name="testkey" val="testval"/>
<button type="submit">TEST</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment