decompress

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2023 License: MIT Imports: 7 Imported by: 0

README

decompress-roundtripper

RoundTripper is an implementation of the http.RoundTripper, that automatically decompresses the response body according to the Content-Encoding header

Installation

$ go get github.com/kei2100/decompress-roundtripper

Example

package decompress_test

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/kei2100/decompress-roundtripper"
)

func ExampleRoundTripper() {
	cli := http.Client{
		// decompress.RoundTripper is automatically decompresses the response body according to the Content-Encoding header
		Transport: &decompress.RoundTripper{},
	}
	svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.Header().Set("Content-Encoding", "gzip")
		b := gzipBytes([]byte("foobarbaz"))
		w.Write(b)
	}))
	req, _ := http.NewRequest("GET", svr.URL, nil)
	req.Header.Set("Accept-Encoding", "gzip")
	resp, _ := cli.Do(req)
	b, _ := io.ReadAll(resp.Body)
	resp.Body.Close()
	fmt.Println(string(b))

	// Output: foobarbaz
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrUnsupportedEncoding

type ErrUnsupportedEncoding struct {
	// original http response
	Original *http.Response
	Encoding string
}

ErrUnsupportedEncoding represents unsupported encoding error

func (*ErrUnsupportedEncoding) Error

func (e *ErrUnsupportedEncoding) Error() string

Error implements the error interface

type RoundTripper

type RoundTripper struct {
	// Wrap is the actual RoundTripper. If Wrap is nil, http.DefaultTransport will be used
	Wrap http.RoundTripper
}

RoundTripper is an implementation of the http.RoundTripper, that automatically decompresses the response body according to the Content-Encoding header

Example
cli := http.Client{
	// decompress.RoundTripper is automatically decompresses the response body according to the Content-Encoding header
	Transport: &decompress.RoundTripper{},
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
	w.Header().Set("Content-Encoding", "gzip")
	b := gzipBytes([]byte("foobarbaz"))
	w.Write(b)
}))
req, _ := http.NewRequest("GET", svr.URL, nil)
req.Header.Set("Accept-Encoding", "gzip")
resp, _ := cli.Do(req)
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
fmt.Println(string(b))
Output:

foobarbaz

func (*RoundTripper) RoundTrip

func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTrip method of the http.RoundTripper. If the response body is compressed, decompress it according to the Content-Encoding header before returning it. Supported Content-Encoding is:

  • gzip
  • deflate
  • br
  • identity

If an unsupported value is set, ErrUnsupportedEncoding will be returned. You can retrieve the original http.Response from ErrUnsupportedEncoding.

Jump to

Keyboard shortcuts

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