headermodification

package module
v0.0.0-...-2457ba2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2021 License: MIT Imports: 1 Imported by: 0

README

headermodification

A simple library to control request headers when you control the http.Client, but not necessarily the request.

This library provides an http.RoundTripper implementation that will Set and Add headers on the outbound request.

Use

The following snippet configures the Transport on an http.Client and then provides that client to an API layer that performs the requests. Because the API layer controls the requests, we would normally not be able to control the outbound headers.

package main

import (
    "net/http"

    "github.com/nabowler/headermodification"
)

func main() {
    transport := headermodification.Transport {
        Set: http.Header{"User-Agent": []string{"my custom useragent"}},
        Add: http.Header{"X-Custom-1": []string{"some custom header"}},
    }

    client := http.Client {
        Transport: transport,
        Timeout:   30 * time.Second,
    }

    api(client)
}


func api(client http.Client) {
    req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, "https://example.com", nil)
    if err != nil {
        // TODO
    }

    resp, err := client.Do(req)
    if err != nil {
        // TODO
    }
    defer resp.Body.Close()
}
Global

The following snippet configures the Transport as http.DefaultTransport. This is necessary because the API controls both the http.Client and the requests, so normally we would have no control over the requests. By hijacking the http.DefaultTransport global variable, most HTTP requests from the application will have their header's modified.

This is not recommended unless necessary and should only ever be configured as such within func main(). This pattern must be used with caution since this may affect all HTTP requests from your application and not just those of a single API.

package main

import (
    "net/http"

    "github.com/nabowler/headermodification"
)

func main() {
    transport := headermodification.Transport {
        Base: http.DefaultTransport,
        Set: http.Header{"User-Agent": []string{"my custom useragent"}},
        Add: http.Header{"X-Custom-1": []string{"some custom header"}},
    }

    http.DefaultTransport = transport

    api()
}


func api() {
    client := http.Client {
        Timeout:   30 * time.Second,
    }

    req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, "https://example.com", nil)
    if err != nil {
        // TODO
    }

    resp, err := client.Do(req)
    if err != nil {
        // TODO
    }
    defer resp.Body.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModifyRequestHeaders

func ModifyRequestHeaders(req *http.Request, set, add http.Header)

ModifyRequestHeaders directly modifies the supplied request's headers. Existing headers will be overwritten with the values in set. Existing headers, including those set here, will be appeneded to with the values in add. Headers that are not in set or add will remain unmodified.

Types

type Transport

type Transport struct {
	// Base is the RoundTripper that will be used after the request headers
	// are modified. If nil, http.DefaultTransport will be used.
	Base http.RoundTripper
	// Set contains the headers that will be overwritten using `http.Header.Set`.
	Set http.Header
	// Add contains the headers that will be appended using `http.Header.Add`.
	Add http.Header
}

Transport will modify the headers of the out-bound request using the values in Set and Add before using Base to perform the RoundTrip. Values in Set will be applied before values in Add.

func (Transport) RoundTrip

func (t Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip modifies the headers of the out-bound request using the values in Set and Add before using Base to perform the RoundTrip. If Base is not set, http.DefaultTransport will be used. If both Set and Add are empty, the request is sent unmodified. If Set or Add have values, the request is cloned before modification, and the clone will be modified (per the RoundTripper contract) and sent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL