Last active
April 24, 2025 14:46
-
-
Save GauthamBanasandra/8d6f27e25a4bb1a805e11716aa4bb66c to your computer and use it in GitHub Desktop.
A simple HTTP server in Golang which just prints all the headers
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
package main | |
import ( | |
"net/http" | |
"fmt" | |
"time" | |
) | |
func handler(w http.ResponseWriter, r *http.Request) { | |
fmt.Printf("Request at %v\n", time.Now()) | |
for k, v := range r.Header { | |
fmt.Printf("%v: %v\n", k, v) | |
} | |
if r.Header.Get("Authorization") == "" { | |
w.WriteHeader(http.StatusUnauthorized) | |
w.Header().Set("WWW-Authenticate", `Digest realm="[email protected]", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"`) | |
} | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":9090", nil) | |
} |
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
package main | |
import ( | |
"net/http" | |
"fmt" | |
"time" | |
) | |
func handler(w http.ResponseWriter, r *http.Request) { | |
fmt.Printf("Request at %v\n", time.Now()) | |
for k, v := range r.Header { | |
fmt.Printf("%v: %v\n", k, v) | |
} | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":9090", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The example is unfortunately incorrect. The order of
w.WriteHeader(http.StatusUnauthorized)
andw.Header().Set("WWW-Authenticate", ...
should be swapped: