Package httpretty prints the HTTP requests you make with Go pretty on your terminal.

httpretty

GoDoc Build Status Coverage Status Go Report Card CII Best Practices

Package httpretty prints the HTTP requests of your Go programs pretty on your terminal screen. It is mostly inspired in curl's --verbose mode, and also on the httputil.DumpRequest and similar functions.

asciicast

Setting up a logger

You can define a logger with something like

logger := &httpretty.Logger{
	Time:           true,
	TLS:            true,
	RequestHeader:  true,
	RequestBody:    true,
	ResponseHeader: true,
	ResponseBody:   true,
	Colors:         true, // erase line if you don't like colors
	Formatters:     []httpretty.Formatter{&httpretty.JSONFormatter{}},
}

This code will set up a logger with sane settings. By default the logger prints nothing but the request line (and the remote address, when using it on the server-side).

Using on the client-side

You can set the transport for the *net/http.Client you are using like this:

client := &http.Client{
	Transport: logger.RoundTripper(http.DefaultTransport),
}

// from now on, you can use client.Do, client.Get, etc. to create requests.

If you don't care about setting a new client, you can safely replace your existing http.DefaultClient with this:

http.DefaultClient.Transport = logger.RoundTripper(http.DefaultClient.Transport)

Then httpretty is going to print information about regular requests to your terminal when code such as this is called:

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

However, have in mind you usually want to use a custom *http.Client to control things such as timeout.

Logging on the server-side

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

logger.Middleware(mux)

The handler should by a http.Handler. Usually, you want this to be your http.ServeMux HTTP entrypoint.

For working examples, please see the example directory.

Filtering

You have two ways to filter a request so it isn't printed by the logger.

httpretty.WithHide

You can filter any request by setting a request context before the request reaches httpretty.RoundTripper:

req = req.WithContext(httpretty.WithHide(ctx))

Filter function

A second option is to implement

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

and set it as the filter for your logger. For example:

logger.SetFilter(func filteredURIs(req *http.Request) (bool, error) {
	if req.Method != http.MethodGet {
		return true, nil
	}

	if path := req.URL.Path; path == "/debug" || strings.HasPrefix(path, "/debug/") {
		return true, nil
	}

	return false
})

Formatters

You can define a formatter for any media type by implementing the Formatter interface.

We provide a JSONFormatter for convenience (it is not enabled by default).

Owner
Henrique Vicente
Senior Software Engineer
Henrique Vicente
Comments
  • Fix tests for architectures without AES hardware support

    Fix tests for architectures without AES hardware support

    https://github.com/golang/go/blob/4aa1efed4853ea067d665a952eee77c52faac774/src/crypto/tls/cipher_suites.go#L342-L355

    Starting with Go 1.16, ChaCha (TLS_CHACHA20_POLY1305_SHA256) is picked over AES (TLS_AES_128_GCM_SHA256) for TLS 1.3 on machines that do not have AES hardware support, e.g. on armhf, 386 and ppc64le, as caught by Debian autopkgtest for golang-github-henvic-httpretty/0.0.6-2 between November 2021 and March 2022.

    The test failure can be reproduced locally with GOARCH=386 go test ./...

    Reference code for different preferred TLS 1.3 ciphers in Go 1.18: https://github.com/golang/go/blob/go1.18/src/crypto/tls/cipher_suites.go#L342-L367 https://github.com/golang/go/blob/go1.18/src/crypto/tls/handshake_client.go#L124-L129

    This commit accommodates TLS_CHACHA20_POLY1305_SHA256 in addition to TLS_AES_128_GCM_SHA256 in the TLS 1.3 tests.

  • Request path interpreted as format string

    Request path interpreted as format string

    Making a request with a query parameter with value foo & bar results in something like this being printed:

    > POST /mypath?label=foo+%!+(MISSING)bar HTTP/1.1
    

    This is because the & character gets %-encoded when in a URL, and color.Format() interprets anything passed to it as a format string. Any value that could potentially have a literal % character in it (any dynamic or user-defined value, really) should never be used a sprintf format string.

  • Response without Content-Length might not print completely

    Response without Content-Length might not print completely

    io.ReadFull should be used instead of calling *bufio.Reader.Read when getting content to print from on the logger printer, as more than one Read call might be required to fill the whole printing buffer.

  • Let user hide unwanted headers

    Let user hide unwanted headers

    On https://github.com/cli/cli/pull/306#pullrequestreview-361869659 @mislav asked me for

    a way to avoid certain request/response headers being printed; perhaps accept a func that takes a header name and returns a boolean that indicates whether to print or not?

    This might be achieved with a function similar to SetFilter and SetBodyFilter. It might take a slice and store the data in a map[string]struct{} to be checked when printing headers.

    Probably can be called FilterHeader or FilterHeaders.

  • TestOutgoingTLSBadClientCertificate seems flaky on Travis CI

    TestOutgoingTLSBadClientCertificate seems flaky on Travis CI

    From https://travis-ci.org/henvic/httpretty/jobs/645236419

    --- FAIL: TestOutgoingTLSBadClientCertificate (0.03s)
    262    client_test.go:1411: logged HTTP request * Request to https://127.0.0.1:35251
    263        * Client certificate:
    264        *  subject: CN=User,OU=User,O=Client,L=Rotterdam,ST=Zuid-Holland,C=NL
    265        *  start date: Sat Jan 25 20:12:36 UTC 2020
    266        *  expire date: Mon Jan  1 20:12:36 UTC 2120
    267        *  issuer: CN=User,OU=User,O=Client,L=Rotterdam,ST=Zuid-Holland,C=NL
    268        > GET / HTTP/1.1
    269        > Host: example.com
    270        > User-Agent: Robot/0.1 [email protected]
    271        
    272        * readLoopPeekFailLocked: remote error: tls: bad certificate
    273        ; want * Request to https://127.0.0.1:35251
    274        * Client certificate:
    275        *  subject: CN=User,OU=User,O=Client,L=Rotterdam,ST=Zuid-Holland,C=NL
    276        *  start date: Sat Jan 25 20:12:36 UTC 2020
    277        *  expire date: Mon Jan  1 20:12:36 UTC 2120
    278        *  issuer: CN=User,OU=User,O=Client,L=Rotterdam,ST=Zuid-Holland,C=NL
    279        > GET / HTTP/1.1
    280        > Host: example.com
    281        > User-Agent: Robot/0.1 [email protected]
    282        
    283        * remote error: tls: bad certificate
    
  • tests: fix tests broken due to TLS CommonName deprecation.

    tests: fix tests broken due to TLS CommonName deprecation.

    CN has been deprecated for 20 years now, but I relied on it for the certificates generated with openssl.

    Go 1.15 crypto/x509 is dropping support for it.

    The environment variable GODEBUG=x509ignoreCN=0 is required for it to work now (until Go 1.16).

    No production code was affected by this change as x509.*Certificate.VerifyHostname is used directly there to retrieve TLS hostname details.

    Related: https://github.com/golang/go/issues/39568#issuecomment-671424481 https://go-review.googlesource.com/c/go/+/243221

    See also: Extended Key Usage section of x509v3_config Let's Encrypt Making and trusting your own certificates

  • Don't try to print logs for mediatypes such as application/octet-stream, images, etc.

    Don't try to print logs for mediatypes such as application/octet-stream, images, etc.

    This can be done both by identifying Content-Type and by inspecting file magic numbers to identify if a file is "binary" or not. Decide what to use (probably both).

    Maybe we can introduce an internal Formatter implementation that identifies the file by matching mime-type and this might be good at first (and use a second step to identify other cases to avoid printing rubbish). However, we might want to identify it earlier and avoid some processing.

  • Custom header sanitization

    Custom header sanitization

    I have a module that calls an API with a custom authorization header. I would like to sanitize it rather than hide it, but currently doesn't appear I can do so. The ability to add additional headers for sanitization would be a great addition.

Colored pretty printer for Go language
Colored pretty printer for Go language

pp Colored pretty printer for Go language Usage Just call pp.Print(). import "github.com/k0kubun/pp" m := map[string]string{"foo": "bar", "hello": "w

Dec 29, 2022
Litter is a pretty printer library for Go data structures to aid in debugging and testing.

Litter Litter is a pretty printer library for Go data structures to aid in debugging and testing. Litter is provided by Sanity: The Headless CMS Const

Dec 28, 2022
🪵 A dead simple, pretty, and feature-rich logger for golang
🪵 A dead simple, pretty, and feature-rich logger for golang

?? lumber ?? A dead simple, pretty, and feature-rich logger for golang ?? Install ?? Logging Functions lumber.Success() lumber.Info() lumber.Debug() l

Jul 20, 2022
Implements a deep pretty printer for Go data structures to aid in debugging

spew Spew implements a deep pretty printer for Go data structures to aid in debugging. A comprehensive suite of tests with 100% test coverage is provi

Dec 25, 2022
With this package you can create your own syslog server with your own handlers for different kind of syslog messages

Using this library you can easy implement your own syslog server that: Can listen on multiple UDP ports and unix domain sockets. Can pass parsed syslo

Nov 9, 2022
Metrics dashboards on terminal (a grafana inspired terminal version)

Grafterm Visualize metrics dashboards on the terminal, like a simplified and minimalist version of Grafana for terminal. Features Multiple widgets (gr

Jan 6, 2023
Scraping medium blogs to make them loadable with shitty internet and have a pleasant reading experience

Unmedium This project is still WIP We all know medium right? A bunch of JS, wast

Mar 20, 2022
Make HMCL working in Apple Silicon Mac without x86 Java

M1MC Apple have used arm64 architecture on their new Macs. But Minecraft have no

Sep 19, 2022
Filez - A tiny package showing you File info

filez A tiny package showing you File info Install go get -v github.com/Cne3Rd/f

Feb 4, 2022
Feb 9, 2022
Package for easy logging to logstash http input from microservices

Micro Logger package for easy logging to logstash http input from microservices

Dec 28, 2021
BRUS - Parses your web server (e.g. nginx) log files and checks with GreyNoise how much noise your website is exposed to.

BRUS bbbbbb rrrrrr u u sssss b b r r u u s bbbbbb rrrrrr u u sssss b b r r u u s bbbbbb r r

May 29, 2022
So you always leave a note
So you always leave a note

jWalterWeatherman Seamless printing to the terminal (stdout) and logging to a io.Writer (file) that’s as easy to use as fmt.Println. Graphic by JonnyE

Dec 19, 2022
Parametrized JSON logging library in Golang which lets you obfuscate sensitive data and marshal any kind of content.
Parametrized JSON logging library in Golang which lets you obfuscate sensitive data and marshal any kind of content.

Noodlog Summary Noodlog is a Golang JSON parametrized and highly configurable logging library. It allows you to: print go structs as JSON messages; pr

Oct 27, 2022
Pixie gives you instant visibility by giving access to metrics, events, traces and logs without changing code.
Pixie gives you instant visibility by giving access to metrics, events, traces and logs without changing code.

Pixie gives you instant visibility by giving access to metrics, events, traces and logs without changing code.

Jan 4, 2023
Gowl is a process management and process monitoring tool at once. An infinite worker pool gives you the ability to control the pool and processes and monitor their status.
Gowl is a process management and process monitoring tool at once. An infinite worker pool gives you the ability to control the pool and processes and monitor their status.

Gowl is a process management and process monitoring tool at once. An infinite worker pool gives you the ability to control the pool and processes and monitor their status.

Nov 10, 2022
ChangeTower is intended to help you watch changes in webpages and get notified of any changes written in Go

ChangeTower is intended to help you watch changes in webpages and get notified of any changes written in Go

Nov 17, 2022
You can send massage to Discord, Slack and Telegram

introduction This package divided into three different packages which includes Telegram, Slack and Discord. Let's begin with Discord Discord To use di

Dec 7, 2021
xlog is a logger for net/context aware HTTP applications
xlog is a logger for net/context aware HTTP applications

⚠️ Check zerolog, the successor of xlog. HTTP Handler Logger xlog is a logger for net/context aware HTTP applications. Unlike most loggers, xlog will

Sep 26, 2022