basicauth

package module
v0.0.0-...-2773524 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 2 Imported by: 1

README

basicauth Go Reference

Provides a simple http.RoundTripper implementation for injecting HTTP Basic Authentication to each request:

package main

import (
	"io"
	"log"
	"net/http"
	"os"

	"github.com/bored-engineer/basicauth"
)

func main() {
	transport := &basicauth.Transport{
		Username: "bored-engineer",
		Password: "hunter2",
	}

	resp, err := (&http.Client{
		Transport: transport,
	}).Get("https://httpbin.org/basic-auth/bored-engineer/hunter2")
	if err != nil {
		log.Fatalf("(*http.Client).Do failed: %v", err)
	}
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("(*http.Response).Body.Read failed: %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		log.Fatalf("http.StatusCode(%d) != http.StatusOK", resp.StatusCode)
	}
	if _, err := os.Stdout.Write(body); err != nil {
		log.Fatalf("os.Stdout.Write failed: %v", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Transport

type Transport struct {
	// Username is the username for HTTP Basic Authentication
	Username string
	// Password is the password for HTTP Basic Authentication
	Password string
	// Base is the base RoundTripper used to make HTTP requests.
	// If nil, http.DefaultTransport is used.
	Base http.RoundTripper
}

Transport adds HTTP Basic Authentication to each HTTP request

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL