• Stars
    star
    385
  • Rank 107,479 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

httpretty

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

More Repositories

1

pgxtutorial

Example of how to build a web service using Go, PostgreSQL, and gRPC
Go
114
star
2

pgq

pgq is a query builder for PostgreSQL written in Go.
Go
39
star
3

picel

picel is a light-weight, blazing fast REST-ful micro service for image processing with a lean API
Go
32
star
4

osx-ulimit

JavaScript
11
star
5

clino

Package clino provides a simple way to create CLI (command-line interface) tools with multiple commands. πŸΆπŸ•πŸΎπŸΎ
Go
11
star
6

require-time

Timer to catch your package slow loading dependencies.
JavaScript
10
star
7

ToggleExclude

Conditional exclude patterns to allow you to browse code faster in Sublime Text.
Python
9
star
8

pgtools

Code and infrastructure for working more effectively with PostgreSQL and Go using pgx, tern, and scany.
Go
8
star
9

birthday

8
star
10

vendorlicenses

vendorlicenses is a tool to check and concatenate licenses found on the vendor directory of Go programs.
Go
7
star
11

rcarweb

Control a RC car with Arduino, node.js and WebSockets
JavaScript
7
star
12

kubeapply

kubeapply is a microservice for running kubectl apply through a web API. This software was built for WeDeploy https://www.wedeploy.com and Liferay DXP Cloud https://www.liferay.com/en-US/products/dxp-cloud
Go
5
star
13

pseudoterm

Framework for running iterative programs programmatically with Go. October 2017 Notice: Google created Expect for Go and it is better than this, therefore the development of this tool is discontinued https://github.com/google/goexpect
Go
5
star
14

climetrics

CLI metrics is a software used for gathering diagnostics and metrics data for Command-Line Interface tools.
Go
4
star
15

vehikel

car ad website project. Project by Henrique Vicente and Rafael Daher.
PHP
4
star
16

ctxsignal

DEPRECATED: Use signal.NotifyContext, available since Go 1.16. Package ctxsignal can be used to create contexts cancelable by system signals. See https://github.com/golang/go/issues/37255
Go
3
star
17

socketio-talk

Talk about a socket.io client for Go https://github.com/henvic/socketio
Go
2
star
18

MediaLab

This is my pet project, it originated www.plifk.com and had two other children (trazqueeupago.com and http://to-post.it/)
PHP
2
star
19

phantom-casper-simple-talk

Talk on http://www.eventick.com.br/2nd-open-meeting-pernambucojs
JavaScript
2
star
20

tel

OpenTelemetry API wrapper to make using opentelemetry-go more idiomatic
Go
1
star
21

wedeploycli

Code for the old WeDeploy/Liferay Cloud Platform CLI (legacy, unmaintained)
Go
1
star
22

write-in-go

1
star
23

productreview

Go
1
star
24

henvic

1
star
25

trigram

Go
1
star
26

picel-js

picel accompanying tool for encoding images
JavaScript
1
star
27

galaxy

Go
1
star