Documentation
¶
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/bingoohuang/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.
Index ¶
- func Ksuid() string
- func WithHide(ctx context.Context) context.Context
- type BodyFilter
- type Filter
- type Flusher
- type Formatter
- type JSONFormatter
- type Logger
- func (l *Logger) Middleware(next http.Handler, printReqID bool) http.Handler
- func (l *Logger) PrintRequest(req *http.Request)
- func (l *Logger) PrintResponse(resp *http.Response)
- func (l *Logger) RoundTripper(rt http.RoundTripper, printReqID bool) http.RoundTripper
- func (l *Logger) SetBodyFilter(f BodyFilter)
- func (l *Logger) SetFilter(f Filter)
- func (l *Logger) SetFlusher(f Flusher)
- func (l *Logger) SetOutput(w io.Writer)
- func (l *Logger) SkipHeader(headers []string)
- Bugs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
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 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 Flusher ¶
type Flusher int
Flusher defines how logger prints requests.
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 )
Logger can print without flushing, when they are available, or when the request is done.
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 JSONFormatter ¶
type JSONFormatter struct{}
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.
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 ¶
type Logger struct { // 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 // 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 // contains filtered or unexported fields }
Logger provides a way for you to print client and server-side information about your HTTP traffic.
func (*Logger) Middleware ¶
Middleware for logging incoming requests to a HTTP server.
func (*Logger) PrintRequest ¶
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 ¶
PrintResponse prints a response.
func (*Logger) RoundTripper ¶
func (l *Logger) RoundTripper(rt http.RoundTripper, printReqID bool) 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 ¶
SetFilter allows you to set a function to skip requests. Pass nil to remove the filter. This method is concurrency safe.
func (*Logger) SetFlusher ¶
SetFlusher sets the flush strategy for the logger.
func (*Logger) SkipHeader ¶
SkipHeader allows you to skip printing specific headers. This method is concurrency safe.
Notes ¶
Bugs ¶
Currently, the TLS information prints after the response header, although it should be printed before the request header.
net/http data race condition when the client does concurrent requests using the very same HTTP transport. See Go standard library issue https://golang.org/issue/30597
Directories
¶
Path | Synopsis |
---|---|
example
|
|
Package httpsnoop provides an easy way to capture http related metrics (i.e.
|
Package httpsnoop provides an easy way to capture http related metrics (i.e. |
internal
|
|
color
Package color can be used to add color to your terminal using ANSI escape code (or sequences).
|
Package color can be used to add color to your terminal using ANSI escape code (or sequences). |
header
Package header can be used to sanitize HTTP request and response headers.
|
Package header can be used to sanitize HTTP request and response headers. |