Created
March 20, 2025 18:32
-
-
Save leninhasda/e3de033d875470924c6b71e09b9bf5d8 to your computer and use it in GitHub Desktop.
Custom method on HTTP request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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