Golang net/http.Request.SetBasicAuth() function example
package net/http
Golang net/http.Request.SetBasicAuth() function usage example
package main
import (
"fmt"
"net/http"
)
func home(w http.ResponseWriter, r *http.Request) {
r.SetBasicAuth("username", "password") // not encrypted!
w.Header().Set("WWW-Authenticate", `Basic realm="Beware! Protected REALM! "`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
username, password, ok := r.BasicAuth()
fmt.Println("username : ", username)
fmt.Println("password : ", password)
fmt.Println("ok : ", ok)
}
func main() {
http.HandleFunc("/", home)
http.ListenAndServe(":8080", nil)
}
References :
http://golang.org/pkg/net/http/#Request.SetBasicAuth
https://www.socketloop.com/references/golang-net-http-request-basicauth-function-example
See also : Golang net/http.Request.BasicAuth() function example
Advertisement
Something interesting
Tutorials
+11.7k How to tell if a binary(executable) file or web application is built with Golang?
+5.4k Golang : Return multiple values from function
+9.7k Golang : Populate slice with sequential integers example
+7k Golang : constant 20013 overflows byte error message
+18.7k Golang : Implement getters and setters
+18.5k Golang : Set, Get and List environment variables
+17.3k Golang : How to tell if a file is compressed either gzip or zip ?
+6.7k Golang : Humanize and Titleize functions
+7.5k Golang : Shuffle strings array
+11.5k Golang : Handle API query by curl with Gorilla Queries example
+10k Golang : Read file and convert content to string
+9k Golang : Inject/embed Javascript before sending out to browser example