All Projects → henvic → Httpretty

henvic / Httpretty

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

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Labels

Projects that are alternatives of or similar to Httpretty

Go Carpet
go-carpet - show test coverage in terminal for Go source files
Stars: ✭ 210 (-1.41%)
Mutual labels:  cli
Pollinate
Template your base files and generate new projects from Git(Hub).
Stars: ✭ 213 (+0%)
Mutual labels:  cli
Humblebundle Downloader
Download you Humble Bundle Library
Stars: ✭ 213 (+0%)
Mutual labels:  cli
Hdfs
API and command line interface for HDFS
Stars: ✭ 209 (-1.88%)
Mutual labels:  cli
Givegif
GIFs on the command line
Stars: ✭ 212 (-0.47%)
Mutual labels:  cli
Atcoder Cli
AtCoder command line tools
Stars: ✭ 213 (+0%)
Mutual labels:  cli
Vsh
vsh - HashiCorp Vault interactive shell and cli tool
Stars: ✭ 209 (-1.88%)
Mutual labels:  cli
Webpack Command
[DEPRECATED] Lightweight, modular, and opinionated webpack CLI that provides a superior experience
Stars: ✭ 218 (+2.35%)
Mutual labels:  cli
Datauri
Generate Data-URI scheme via terminal or node.js
Stars: ✭ 212 (-0.47%)
Mutual labels:  cli
Zoe
The Kafka CLI for humans
Stars: ✭ 217 (+1.88%)
Mutual labels:  cli
Tldr
Golang command line client for tldr https://github.com/tldr-pages/tldr
Stars: ✭ 210 (-1.41%)
Mutual labels:  cli
Dry Cli
General purpose Command Line Interface (CLI) framework for Ruby
Stars: ✭ 210 (-1.41%)
Mutual labels:  cli
Csview
📠 A high performance csv viewer with cjk/emoji support.
Stars: ✭ 208 (-2.35%)
Mutual labels:  cli
Json 2 Csv
Convert JSON to CSV *or* CSV to JSON!
Stars: ✭ 210 (-1.41%)
Mutual labels:  cli
Popper
Container-native task automation engine.
Stars: ✭ 216 (+1.41%)
Mutual labels:  cli
Xcolor
Lightweight color picker for X11
Stars: ✭ 209 (-1.88%)
Mutual labels:  cli
Octosql
OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases and file formats using SQL.
Stars: ✭ 2,579 (+1110.8%)
Mutual labels:  cli
Kmdr Cli
🧠 The CLI tool for learning commands from your terminal
Stars: ✭ 218 (+2.35%)
Mutual labels:  cli
Search Engine Parser
Lightweight package to query popular search engines and scrape for result titles, links and descriptions
Stars: ✭ 216 (+1.41%)
Mutual labels:  cli
Sitemap Generator Cli
Creates an XML-Sitemap by crawling a given site.
Stars: ✭ 214 (+0.47%)
Mutual labels:  cli

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).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].