Skip to content

Instantly share code, notes, and snippets.

@leninhasda
Created March 20, 2025 18:32
Show Gist options
  • Save leninhasda/e3de033d875470924c6b71e09b9bf5d8 to your computer and use it in GitHub Desktop.
Save leninhasda/e3de033d875470924c6b71e09b9bf5d8 to your computer and use it in GitHub Desktop.
Custom method on HTTP request
// server.go
package main
import "net/http"
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "LENIN" { // only allow LENIN method :3
w.WriteHeader(501)
w.Write([]byte(`Method not implemented`))
return
}
w.WriteHeader(200)
w.Write([]byte(`Hi Lenin!`))
return
})
http.ListenAndServe(":8000", mux)
}
// client.go
package main
import (
"fmt"
"net/http"
"net/http/httputil"
)
func main() {
url := "http://localhost:8000/"
req, _ := http.NewRequest("LENIN", url, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
b, _ := httputil.DumpResponse(resp, true)
fmt.Println(string(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment