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