response

package
v0.0.0-...-202847b Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package response provides functions for working with an http response.

Every write operation to the ResponseWriter returns the error behaviour WriteFailed.

TODO(cs) => github.com/streadway/handy

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Print

type Print struct {
	// FileSystem stubbed out for testing. Default http.Dir
	FileSystem http.FileSystem
	Response   http.ResponseWriter
	Request    *http.Request
	Renderer   interface {
		// ExecuteTemplate is the interface function to text/template or html/template
		ExecuteTemplate(wr io.Writer, name string, data interface{}) error
	}
}

Print is a helper type for outputting data to a ResponseWriter. Print act as a non-pointer type. Print functions uses internally a byte buffer pool.

func NewPrinter

func NewPrinter(w http.ResponseWriter, r *http.Request) Print

NewPrinter creates a non-pointer printer

func (Print) File

func (p Print) File(path, name string, attachment bool) error

File sends a response with the content of the file. If `attachment` is set to true, the client is prompted to save the file with provided `name`, name can be empty, in that case name of the file is used.

func (Print) HTML

func (p Print) HTML(code int, format string, a ...interface{}) error

HTML formats according to a format specifier and sends HTML response with status code.

func (Print) JSON

func (p Print) JSON(code int, i interface{}) (err error)

JSON sends a JSON response with status code.

func (Print) JSONIndent

func (p Print) JSONIndent(code int, i interface{}, prefix string, indent string) (err error)

JSONIndent sends a JSON response with status code, but it applies prefix and indent to format the output.

func (Print) JSONP

func (p Print) JSONP(code int, callback string, i interface{}) error

JSONP sends a JSONP response with status code. It uses `callback` to construct the JSONP payload.

func (Print) NoContent

func (p Print) NoContent(code int) error

NoContent sends a response with no body and a status code.

func (Print) Redirect

func (p Print) Redirect(code int, url string) error

Redirect redirects the request using http.Redirect with status code.

func (Print) Render

func (p Print) Render(code int, name string, data interface{}) error

Render renders a template with data and sends a text/html response with status code. Templates can be registered during `Print` creation.

func (Print) String

func (p Print) String(code int, format string, a ...interface{}) error

String formats according to a format specifier and sends text response with status code.

func (Print) WriteString

func (p Print) WriteString(code int, s string) error

WriteString converts a string into []bytes and outputs it. No formatting feature available.

func (Print) XML

func (p Print) XML(code int, i interface{}) (err error)

XML sends an XML response with status code.

func (Print) XMLIndent

func (p Print) XMLIndent(code int, i interface{}, prefix string, indent string) (err error)

XMLIndent sends an XML response with status code, but it applies prefix and indent to format the output.

Jump to

Keyboard shortcuts

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