httpwrite

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2021 License: MIT Imports: 2 Imported by: 0

README

httpwrite

.github/workflows/httpwrite.yaml

Render HTTP response in one step.

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

Usage

// Use builder to build response, don't worry about http.ResponseWriter call orders.
httpwrite.Render(rw, 
    httpwrite.Options().
    	JSON(payload).
        WithStatus(201).
    	AddHeader("Custom-Header", "hello"),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(rw http.ResponseWriter, options *WriteOptions) error

Render renders the response according to WriteOptions.

Types

type WriteOptions

type WriteOptions struct {
	// contains filtered or unexported fields
}

func Options

func Options() *WriteOptions

Options is the entrypoint for configuration WriteOptions. By default, it assumes 200 status and plain body

func (*WriteOptions) AddHeaders

func (s *WriteOptions) AddHeaders(kvs ...string) *WriteOptions

AddHeaders adds the key value pairs to response headers. If the pairs are not even numbered, the method panics.

func (*WriteOptions) Form

func (s *WriteOptions) Form(payload interface{}) *WriteOptions

Form sets the payload and also the "Content-Type" header to "application/x-www-form-urlencoded". If payload is nil, this method is noop. Check github.com/absurdlab/pkg/httpcodec for accepted types.

func (*WriteOptions) HTML

func (s *WriteOptions) HTML(payload interface{}) *WriteOptions

HTML sets the payload and also the "Content-Type" header to "text/html". If payload is nil, this method is noop. Check github.com/absurdlab/pkg/httpcodec for accepted types.

func (*WriteOptions) JSON

func (s *WriteOptions) JSON(payload interface{}) *WriteOptions

JSON sets the payload and also the "Content-Type" header to "application/json". If payload is nil, this method is noop.

func (*WriteOptions) PlainText

func (s *WriteOptions) PlainText(payload interface{}) *WriteOptions

PlainText sets the payload and also the "Content-Type" header to "text/plain". If payload is nil, this method is noop. Check github.com/absurdlab/pkg/httpcodec for accepted types.

func (*WriteOptions) WithBody

func (s *WriteOptions) WithBody(body interface{}, codec httpcodec.Encoder) *WriteOptions

WithBody sets custom payload and encoder. If either is nil, this method is noop.

func (*WriteOptions) WithStatus

func (s *WriteOptions) WithStatus(status int) *WriteOptions

WithStatus sets the response status.

func (*WriteOptions) XML

func (s *WriteOptions) XML(payload interface{}) *WriteOptions

XML sets the payload and also the "Content-Type" header to "application/xml". If payload is nil, this method is noop.

Jump to

Keyboard shortcuts

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