httpcodec

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2021 License: MIT Imports: 9 Imported by: 2

README

httpcodec

easy HTTP encoding and decoding.

go get -u github.com/absurdlab/pkg/httpcodec

Features

  • Raw/Plain codec
  • JSON codec
  • XML codec
  • Form codec

Usage

// encode
httpcodec.EncodeRaw(rw, "hello")
httpcodec.EncodeJSON(rw, jsonPayload{Message: "hello"})
httpcodec.EncodeXML(rw, xmlPayload{Message: "hello"})
httpcodec.EncodeForm(rw, map[string]string{"greeting": "hello"})

// decode JSON
httpcodec.DecodeRaw(&stringBuilder)(httpResp)
httpcodec.DecodeJSON(payload)(httpResp)
httpcodec.DecodeXML(payload)(httpResp)
httpcodec.DecodeForm(&urlValues)(httpResp)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeForm

func EncodeForm(writer io.Writer, payload interface{}) error

EncodeForm encodes the payload as URL encoded form into writer. Accepted types include url.Values, map[string][]string, and map[string]string. Other types will error.

func EncodeJSON

func EncodeJSON(writer io.Writer, payload interface{}) error

EncodeJSON is an Encoder that encodes payload as JSON.

func EncodeRaw

func EncodeRaw(rw io.Writer, payload interface{}) error

EncodeRaw is an Encoder that encodes payload as is. Accept payload types are string, []byte, *bytes.Buffer, and *strings.Builder. Any other payload types will error.

func EncodeXML

func EncodeXML(writer io.Writer, payload interface{}) error

EncodeXML encodes payload as XML into writer.

Types

type Decoder

type Decoder func(resp *http.Response) error

Decoder decodes response body into some destination of the caller's choice.

func DecodeForm

func DecodeForm(destination *url.Values) Decoder

DecodeForm decodes response body as URL encoded form into the destination url.Values. The response body is closed afterwards.

func DecodeJSON

func DecodeJSON(destination interface{}) Decoder

DecodeJSON produces a Decoder that decodes response body into given destination. The response body is closed afterwards.

func DecodeRaw

func DecodeRaw(writer io.Writer) Decoder

DecodeRaw decodes the response body into the given writer. Typically, the writer is backed by bytes.Buffer, or strings.Builder. The response body is closed afterwards.

func DecodeXML

func DecodeXML(destination interface{}) Decoder

DecodeXML decodes response body as XML into destination. The decoding charset is read from the XML attribute. The response body is closed afterwards.

type Encoder

type Encoder func(writer io.Writer, payload interface{}) error

Encoder encodes payload into writer.

Jump to

Keyboard shortcuts

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