http

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package http provides a http client that outputs raw http to stdout.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(dialer *net.Dialer, transport *http.Transport) *http.Client

NewClient returns a http.Client that will output all http data to stderr. The client has various default timeouts, call with nil values to use them, otherwise pass arguments to customise.

Example
package main

import (
	"io/ioutil"

	"github.com/petems/client-inspect/http"
)

func main() {
	client := http.NewClient(nil, nil)

	resp, _ := client.Get("http://example.com/")
	// ensure all of the body is read
	ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	resp, _ = client.Get("https://example.com/")
	ioutil.ReadAll(resp.Body)
	resp.Body.Close()

}

func NewClientWriter

func NewClientWriter(dialer *net.Dialer, transport *http.Transport, writer io.Writer) *http.Client

NewClientWriter returns a http.Client that will output all http data to a given io.Writer The client has various default timeouts, call with nil values to use them, otherwise pass arguments to customise.

Example (ToCustomBuffer)
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"strings"

	"github.com/petems/client-inspect/http"
)

func main() {
	buf := new(bytes.Buffer)

	client := http.NewClientWriter(nil, nil, buf)

	resp, _ := client.Get("http://example.com/")
	// ensure all of the body is read
	ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	resp, _ = client.Get("https://example.com/")
	ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	httpLog := buf.String()

	s := strings.Split(httpLog, "\n")

	fmt.Println(s[4])

}
Output:
Host: example.com
Example (ToCustomBufferWithRedaction)
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"regexp"
	"strings"

	"github.com/petems/client-inspect/http"
)

func main() {
	buf := new(bytes.Buffer)

	client := http.NewClientWriter(nil, nil, buf)

	resp, _ := client.Get("http://example.com/")
	// ensure all of the body is read
	ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	resp, _ = client.Get("https://example.com/")
	ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	httpLog := buf.String()

	s := strings.Split(httpLog, "\n")

	for count, line := range s {
		rgx := regexp.MustCompile(`^(Host: )(.+)$`)
		line = rgx.ReplaceAllString(line, `$1[REDACTED]`)
		s[count] = line
	}

	fmt.Println(s[4])
}
Output:
Host: [REDACTED]
Example (ToStderr)
package main

import (
	"io/ioutil"
	"os"

	"github.com/petems/client-inspect/http"
)

func main() {
	client := http.NewClientWriter(nil, nil, os.Stderr)

	resp, _ := client.Get("http://example.com/")
	// ensure all of the body is read
	ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	resp, _ = client.Get("https://example.com/")
	ioutil.ReadAll(resp.Body)
	resp.Body.Close()

}

Types

This section is empty.

Jump to

Keyboard shortcuts

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