...

Text file src/github.com/henvic/httpretty/README.md

Documentation: github.com/henvic/httpretty

     1# httpretty
     2[![GoDoc](https://godoc.org/github.com/henvic/httpretty?status.svg)](https://godoc.org/github.com/henvic/httpretty) [![Build Status](https://travis-ci.org/henvic/httpretty.svg?branch=master)](https://travis-ci.org/henvic/httpretty) [![Coverage Status](https://coveralls.io/repos/henvic/httpretty/badge.svg)](https://coveralls.io/r/henvic/httpretty) [![Go Report Card](https://goreportcard.com/badge/github.com/henvic/httpretty)](https://goreportcard.com/report/github.com/henvic/httpretty) [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/3669/badge)](https://bestpractices.coreinfrastructure.org/projects/3669)
     3
     4Package httpretty prints the HTTP requests of your Go programs pretty on your terminal screen. It is mostly inspired in [curl](https://curl.haxx.se)'s `--verbose` mode, and also on the [httputil.DumpRequest](https://golang.org/pkg/net/http/httputil/) and similar functions.
     5
     6[![asciicast](https://asciinema.org/a/297429.svg)](https://asciinema.org/a/297429)
     7
     8## Setting up a logger
     9You can define a logger with something like
    10
    11```go
    12logger := &httpretty.Logger{
    13	Time:           true,
    14	TLS:            true,
    15	RequestHeader:  true,
    16	RequestBody:    true,
    17	ResponseHeader: true,
    18	ResponseBody:   true,
    19	Colors:         true, // erase line if you don't like colors
    20	Formatters:     []httpretty.Formatter{&httpretty.JSONFormatter{}},
    21}
    22```
    23
    24This 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).
    25
    26### Using on the client-side
    27You can set the transport for the [*net/http.Client](https://golang.org/pkg/net/http/#Client) you are using like this:
    28
    29```go
    30client := &http.Client{
    31	Transport: logger.RoundTripper(http.DefaultTransport),
    32}
    33
    34// from now on, you can use client.Do, client.Get, etc. to create requests.
    35```
    36
    37If you don't care about setting a new client, you can safely replace your existing http.DefaultClient with this:
    38
    39```go
    40http.DefaultClient.Transport = logger.RoundTripper(http.DefaultClient.Transport)
    41```
    42
    43Then httpretty is going to print information about regular requests to your terminal when code such as this is called:
    44```go
    45if _, err := http.Get("https://www.google.com/"); err != nil {
    46        fmt.Fprintf(os.Stderr, "%+v\n", err)
    47        os.Exit(1)
    48}
    49```
    50
    51However, have in mind you usually want to use a custom *http.Client to control things such as timeout.
    52
    53## Logging on the server-side
    54You can use the logger quickly to log requests on your server. For example:
    55
    56```go
    57logger.Middleware(mux)
    58```
    59
    60The handler should by a http.Handler. Usually, you want this to be your `http.ServeMux` HTTP entrypoint.
    61
    62For working examples, please see the example directory.
    63
    64## Filtering
    65You have two ways to filter a request so it isn't printed by the logger.
    66
    67### httpretty.WithHide
    68You can filter any request by setting a request context before the request reaches `httpretty.RoundTripper`:
    69
    70```go
    71req = req.WithContext(httpretty.WithHide(ctx))
    72```
    73
    74### Filter function
    75A second option is to implement
    76
    77```go
    78type Filter func(req *http.Request) (skip bool, err error)
    79```
    80
    81and set it as the filter for your logger. For example:
    82
    83```go
    84logger.SetFilter(func filteredURIs(req *http.Request) (bool, error) {
    85	if req.Method != http.MethodGet {
    86		return true, nil
    87	}
    88
    89	if path := req.URL.Path; path == "/debug" | strings.HasPrefix(path, "/debug/") {
    90		return true, nil
    91	}
    92
    93	return false
    94})
    95```
    96
    97## Formatters
    98You can define a formatter for any media type by implementing the Formatter interface.
    99
   100We provide a JSONFormatter for convenience (it is not enabled by default).

View as plain text