
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
github.com/bingoohuang/ngg/httpretty
Advanced tools
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.
forked from henvic/httpretty
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).
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.
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.
You have two ways to filter a request so it isn't printed by the logger.
You can filter any request by setting a request context before the request reaches httpretty.RoundTripper:
req = req.WithContext(httpretty.WithHide(ctx))
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
})
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).
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.