...

Package httpretty

import "github.com/henvic/httpretty"
Overview
Index
Subdirectories

Overview ▾

Package httpretty prints your HTTP requests pretty on your terminal screen. You can use this package both on the client-side and on the server-side.

This package provides a better way to view HTTP traffic without httputil DumpRequest, DumpRequestOut, and DumpResponse heavy debugging functions.

You can use the logger quickly to log requests you are opening. For example:

package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/henvic/httpretty"
)

func main() {
	logger := &httpretty.Logger{
		Time:           true,
		TLS:            true,
		RequestHeader:  true,
		RequestBody:    true,
		ResponseHeader: true,
		ResponseBody:   true,
		Colors:         true,
		Formatters:     []httpretty.Formatter{&httpretty.JSONFormatter{}},
	}

	http.DefaultClient.Transport = logger.RoundTripper(http.DefaultClient.Transport) // tip: you can use it on any *http.Client

	if _, err := http.Get("https://www.google.com/"); err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}

If you pass nil to the logger.RoundTripper it is going to fallback to http.DefaultTransport.

You can use the logger quickly to log requests on your server. For example:

logger := &httpretty.Logger{
	Time:           true,
	TLS:            true,
	RequestHeader:  true,
	RequestBody:    true,
	ResponseHeader: true,
	ResponseBody:   true,
}

logger.Middleware(handler)

Note: server logs don't include response headers set by the server. Client logs don't include request headers set by the HTTP client.

func WithHide

func WithHide(ctx context.Context) context.Context

WithHide can be used to protect a request from being exposed.

type BodyFilter

BodyFilter allows you to skip printing a HTTP body based on its associated Header.

It can be used for omitting HTTP Request and Response bodies. You can filter by checking properties such as Content-Type or Content-Length.

On a HTTP server, this function is called even when no body is present due to http.Request always carrying a non-nil value.

type BodyFilter func(h http.Header) (skip bool, err error)

type Filter

Filter allows you to skip requests.

If an error happens and you want to log it, you can pass a not-null error value.

type Filter func(req *http.Request) (skip bool, err error)

type Flusher

Flusher defines how logger prints requests.

type Flusher int

Logger can print without flushing, when they are available, or when the request is done.

const (
    // NoBuffer strategy prints anything immediately, without buffering.
    // It has the issue of mingling concurrent requests in unpredictable ways.
    NoBuffer Flusher = iota

    // OnReady buffers and prints each step of the request or response (header, body) whenever they are ready.
    // It reduces mingling caused by mingling but does not give any ordering guarantee, so responses can still be out of order.
    OnReady

    // OnEnd buffers the whole request and flushes it once, in the end.
    OnEnd
)

type Formatter

Formatter can be used to format body.

If the Format function returns an error, the content is printed in verbatim after a warning. Match receives a media type from the Content-Type field. The body is formatted if it returns true.

type Formatter interface {
    Match(mediatype string) bool
    Format(w io.Writer, src []byte) error
}

type JSONFormatter

JSONFormatter helps you read unreadable JSON documents.

github.com/tidwall/pretty could be used to add colors to it. However, it would add an external dependency. If you want, you can define your own formatter using it or anything else. See Formatter.

type JSONFormatter struct{}

func (*JSONFormatter) Format

func (j *JSONFormatter) Format(w io.Writer, src []byte) error

Format JSON content.

func (*JSONFormatter) Match

func (j *JSONFormatter) Match(mediatype string) bool

Match JSON media type.

type Logger

Logger provides a way for you to print client and server-side information about your HTTP traffic.

type Logger struct {
    // SkipRequestInfo avoids printing a line showing the request URI on all requests plus a line
    // containing the remote address on server-side requests.
    SkipRequestInfo bool

    // Time the request began and its duration.
    Time bool

    // TLS information, such as certificates and ciphers.
    // BUG(henvic): Currently, the TLS information prints after the response header, although it
    // should be printed before the request header.
    TLS bool

    // RequestHeader set by the client or received from the server.
    RequestHeader bool

    // RequestBody sent by the client or received by the server.
    RequestBody bool

    // ResponseHeader received by the client or set by the HTTP handlers.
    ResponseHeader bool

    // ResponseBody received by the client or set by the server.
    ResponseBody bool

    // SkipSanitize bypasses sanitizing headers containing credentials (such as Authorization).
    SkipSanitize bool

    // Colors set ANSI escape codes that terminals use to print text in different colors.
    Colors bool

    // Formatters for the request and response bodies.
    // No standard formatters are used. You need to add what you want to use explicitly.
    // We provide a JSONFormatter for convenience (add it manually).
    Formatters []Formatter

    // MaxRequestBody the logger can print.
    // If value is not set and Content-Length is not sent, 4096 bytes is considered.
    MaxRequestBody int64

    // MaxResponseBody the logger can print.
    // If value is not set and Content-Length is not sent, 4096 bytes is considered.
    MaxResponseBody int64
    // contains filtered or unexported fields
}

func (*Logger) Middleware

func (l *Logger) Middleware(next http.Handler) http.Handler

Middleware for logging incoming requests to a HTTP server.

func (*Logger) PrintRequest

func (l *Logger) PrintRequest(req *http.Request)

PrintRequest prints a request, even when WithHide is used to hide it.

It doesn't log TLS connection details or request duration.

func (*Logger) PrintResponse

func (l *Logger) PrintResponse(resp *http.Response)

PrintResponse prints a response.

func (*Logger) RoundTripper

func (l *Logger) RoundTripper(rt http.RoundTripper) http.RoundTripper

RoundTripper returns a RoundTripper that uses the logger.

func (*Logger) SetBodyFilter

func (l *Logger) SetBodyFilter(f BodyFilter)

SetBodyFilter allows you to set a function to skip printing a body. Pass nil to remove the body filter. This method is concurrency safe.

func (*Logger) SetFilter

func (l *Logger) SetFilter(f Filter)

SetFilter allows you to set a function to skip requests. Pass nil to remove the filter. This method is concurrency safe.

func (*Logger) SetFlusher

func (l *Logger) SetFlusher(f Flusher)

SetFlusher sets the flush strategy for the logger.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput sets the output destination for the logger.

func (*Logger) SkipHeader

func (l *Logger) SkipHeader(headers []string)

SkipHeader allows you to skip printing specific headers. This method is concurrency safe.

Subdirectories

Name Synopsis
..
example
client
httprepl
server