Add interceptors to GO http.Client

mediary

Go Report Card Coverage Status Build Status Go package dev

Add interceptors to http.Client and you will be able to

  • Dump request and/or response to a Log
  • Alter your requests before they are sent or responses before they are returned
  • Add Tracing information using Opentracing/Jaeger
  • Send metrics to statsd

All this and more while using a "regular" http.Client

Usage

var client *http.Client
client = mediary.Init().AddInterceptors(your interceptor).Build()
client.Get("https://golang.org")

Dump Example

client := mediary.Init().AddInterceptors(dumpInterceptor).Build()
client.Get("https://golang.org")

func dumpInterceptor(req *http.Request, handler mediary.Handler) (*http.Response, error) {
	if bytes, err := httputil.DumpRequestOut(req, true); err == nil {
		fmt.Printf("%s", bytes)

		//GET / HTTP/1.1
		//Host: golang.org
		//User-Agent: Go-http-client/1.1
		//Accept-Encoding: gzip
	}
	return handler(req)
}

Interceptor

Interceptor is a function

type Interceptor func(*http.Request, Handler) (*http.Response, error)

Handler is just an alias to http.Roundtripper function called RoundTrip

type Handler func(*http.Request) (*http.Response, error)

Multiple interceptors

It's possible to chain interceptors

client := mediary.Init().
    AddInterceptors(First Interceptor, Second Interceptor).
    AddInterceptors(Third Interceptor).
    Build()

This is how it actually works

  • First Intereptor: Request
    • Second Interceptor: Request
      • Third Interceptor: Request
        • Handler <-- Actual http call
      • Third Interceptor: Response
    • Second Interceptor: Response
  • First Interceptor: Response

Using custom client/transport

If you already have a pre-configured http.Client you can use it as well

yourClient := &http.Client{}
yourClientWithInterceptor := mediary.Init().
    WithPreconfiguredClient(yourClient).
    AddInterceptors(your interceptor).
    Build()

Read this for more information

Similar Resources

HTTP/2 Apple Push Notification service (APNs) provider for Go with token-based connection

APNs Provider HTTP/2 Apple Push Notification service (APNs) provider for Go with token-based connection Example: key, err := apns.AuthKeyFromFile("Aut

Dec 29, 2022

OpenID Connect (OIDC) http middleware for Go

Go OpenID Connect (OIDC) HTTP Middleware Introduction This is a middleware for http to make it easy to use OpenID Connect. Currently Supported framewo

Jan 1, 2023

Hex - Expectations for HTTP handlers

Hex is a simple wrapper that extends httptest.Server with an expectation syntax, allowing you to create mock APIs using a simple and expressive DSL:

Aug 9, 2021

Statigz serves pre-compressed embedded files with http in Go

statigz statigz serves pre-compressed embedded files with http in Go 1.16 and later. Why? Since version 1.16 Go provides standard way to embed static

Dec 24, 2022

A Concurrent HTTP Static file server using golang .

A Concurrent HTTP static server using Golang. Serve Static files like HTML,CSS,Js,Images,Videos ,ect. using HTTP. It is Concurrent and Highly Scalable.Try now!

Dec 19, 2021

Go HTTP middleware to filter clients by IP

Go HTTP middleware to filter clients by IP

Oct 30, 2022

A dead simple, stupid, http service.

A dead simple, stupid, http service implemented in a complicated way just for the sake of following Go design patterns and scalability. Useful for learning and testing basic kubernetes networking. Made on an insomniac night.

Sep 2, 2022

A http service to verify request and bounce them according to decisions made by CrowdSec.

traefik-crowdsec-bouncer A http service to verify request and bounce them according to decisions made by CrowdSec. Description This repository aim to

Dec 21, 2022

Chi ip banner is a chi middleware that bans some ips from your Chi http server.

Chi Ip Banner Chi ip banner is a chi middleware that bans some ips from your Chi http server. It reads a .txt file in your project's root, called bani

Jan 4, 2022
Composable chains of nested http.Handler instances.

chain go get github.com/codemodus/chain Package chain aids the composition of nested http.Handler instances. Nesting functions is a simple concept. I

Sep 27, 2022
Go http.Hander based middleware stack with context sharing

wrap Package wrap creates a fast and flexible middleware stack for http.Handlers. Features small; core is only 13 LOC based on http.Handler interface;

Apr 5, 2022
Minimalist net/http middleware for golang

interpose Interpose is a minimalist net/http middleware framework for golang. It uses http.Handler as its core unit of functionality, minimizing compl

Sep 27, 2022
Lightweight Middleware for net/http

MuxChain MuxChain is a small package designed to complement net/http for specifying chains of handlers. With it, you can succinctly compose layers of

Dec 10, 2022
Idiomatic HTTP Middleware for Golang

Negroni Notice: This is the library formerly known as github.com/codegangsta/negroni -- Github will automatically redirect requests to this repository

Jan 2, 2023
A tiny http middleware for Golang with added handlers for common needs.

rye A simple library to support http services. Currently, rye provides a middleware handler which can be used to chain http handlers together while pr

Jan 4, 2023
A collection of useful middleware for Go HTTP services & web applications 🛃

gorilla/handlers Package handlers is a collection of handlers (aka "HTTP middleware") for use with Go's net/http package (or any framework supporting

Dec 31, 2022
Simple middleware to rate-limit HTTP requests.

Tollbooth This is a generic middleware to rate-limit HTTP requests. NOTE 1: This library is considered finished. NOTE 2: Major version changes are bac

Dec 28, 2022
A HTTP mole service
A HTTP mole service

httpmole provides a HTTP mock server that will act as a mole among your services, telling you everything http clients send to it and responding them whatever you want it to respond. Just like an actual mole.

Jul 27, 2022
Mahi is an all-in-one HTTP service for file uploading, processing, serving, and storage.
Mahi is an all-in-one HTTP service for file uploading, processing, serving, and storage.

Mahi is an all-in-one HTTP service for file uploading, processing, serving, and storage. Mahi supports chunked, resumable, and concurrent uploads. Mahi uses Libvips behind the scenes making it extremely fast and memory efficient.

Dec 29, 2022