Standard library / net/http

http.Request.BasicAuth()

Decode Basic credentials; success does not authenticate the user. Expected stdout: 'demo true\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Request) BasicAuth() (username, password string, ok bool)

Return Value

(username, password string, ok bool)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http")
func main() {  r := must(http.NewRequest("GET", "https://example.invalid/", nil))  r.SetBasicAuth("demo", "test-only")  u, _, ok := r.BasicAuth()  fmt.Println(u, ok)}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference