Standard library / net/http

http.Request.SetBasicAuth()

Set the Basic Authorization header using base64, not encryption. Use only with protected transport. Expected stdout: 'demo true\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Request) SetBasicAuth(username, password string)

Parameters & Attributes

NameTypeDescription
usernamestringSynthetic or actual account name.
passwordstringPassword; never log real credentials.

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