Documentation
¶
Overview ¶
Package respond is a http data responder
Source code and other details for the project are available at GitHub:
https://github.com/gookit/respond
usage please see README and examples.
Index ¶
- Constants
- Variables
- func Auto(w http.ResponseWriter, req *http.Request, v interface{}) error
- func Binary(w http.ResponseWriter, status int, in io.Reader, outName string, inline bool) error
- func Content(w http.ResponseWriter, status int, v []byte, contentType string) error
- func Data(w http.ResponseWriter, status int, v interface{}, contentType string) error
- func Empty(w http.ResponseWriter) error
- func HTML(w http.ResponseWriter, status int, template string, v interface{}, ...) error
- func HTMLString(w http.ResponseWriter, status int, tplContent string, v interface{}) error
- func HTMLText(w http.ResponseWriter, status int, html string) error
- func Initialize(config func(opts *Options))
- func JSON(w http.ResponseWriter, status int, v interface{}) error
- func JSONP(w http.ResponseWriter, status int, callback string, v interface{}) error
- func LoadTemplateFiles(files ...string)
- func LoadTemplateGlob(pattern string)
- func NoContent(w http.ResponseWriter) error
- func String(w http.ResponseWriter, status int, v string) error
- func Text(w http.ResponseWriter, status int, v string) error
- func XML(w http.ResponseWriter, status int, v interface{}) error
- type Options
- type Responder
- func (r *Responder) Auto(w http.ResponseWriter, req *http.Request, data interface{}) error
- func (r *Responder) Binary(w http.ResponseWriter, status int, in io.Reader, outName string, inline bool) error
- func (r *Responder) Content(w http.ResponseWriter, status int, v []byte, contentType string) error
- func (r *Responder) Data(w http.ResponseWriter, status int, v interface{}, contentType string) error
- func (r *Responder) Empty(w http.ResponseWriter) error
- func (r *Responder) HTML(w http.ResponseWriter, status int, template string, v interface{}, ...) error
- func (r *Responder) HTMLString(w http.ResponseWriter, status int, tplContent string, v interface{}) error
- func (r *Responder) HTMLText(w http.ResponseWriter, status int, html string) error
- func (r *Responder) Initialize() *Responder
- func (r *Responder) JSON(w http.ResponseWriter, status int, v interface{}) error
- func (r *Responder) JSONP(w http.ResponseWriter, status int, callback string, v interface{}) error
- func (r *Responder) LoadTemplateFiles(files ...string)
- func (r *Responder) LoadTemplateGlob(pattern string)
- func (r *Responder) NoContent(w http.ResponseWriter) error
- func (r *Responder) Options() Options
- func (r *Responder) Renderer() *view.Renderer
- func (r *Responder) String(w http.ResponseWriter, status int, v string) error
- func (r *Responder) Text(w http.ResponseWriter, status int, v string) error
- func (r *Responder) XML(w http.ResponseWriter, status int, v interface{}) error
Constants ¶
const ( // ContentType header key ContentType = "Content-Type" // ContentText represents content type text/plain ContentText = "text/plain" // ContentJSON represents content type application/json ContentJSON = "application/json" // ContentJSONP represents content type application/javascript ContentJSONP = "application/javascript" // ContentXML represents content type application/xml ContentXML = "application/xml" // ContentHTML represents content type text/html ContentHTML = "text/html" // ContentBinary represents content type application/octet-stream ContentBinary = "application/octet-stream" // ContentDisposition describes contentDisposition ContentDisposition = "Content-Disposition" )
Variables ¶
var ( Marshal = json.Marshal MarshalIndent = json.MarshalIndent NewDecoder = json.NewDecoder )
var As = New()
create a default instance
Functions ¶
func Auto ¶
func Auto(w http.ResponseWriter, req *http.Request, v interface{}) error
Auto data response
func Data ¶
func Data(w http.ResponseWriter, status int, v interface{}, contentType string) error
Data data response
func HTML ¶
func HTML(w http.ResponseWriter, status int, template string, v interface{}, layout ...string) error
HTML data response
func HTMLString ¶
func HTMLString(w http.ResponseWriter, status int, tplContent string, v interface{}) error
HTMLString data response
func HTMLText ¶
func HTMLText(w http.ResponseWriter, status int, html string) error
HTMLText data response
Types ¶
type Options ¶
type Options struct {
Debug bool
JSONIndent bool
JSONPrefix string
XMLIndent bool
XMLPrefix string
// template render
TplLayout string
TplDelims view.TplDelims
TplFuncMap template.FuncMap
TplViewsDir string
TplSuffixes []string
// supported content types
ContentBinary, ContentHTML, ContentXML, ContentText, ContentJSON, ContentJSONP string
// Charset default content data charset
Charset string
// AppendCharset on response content
AppendCharset bool
}
Options for the Responder
type Responder ¶
type Responder struct {
// contains filtered or unexported fields
}
Responder definition
func NewInitialized ¶
NewInitialized create new instance and initialization it.
func (*Responder) Binary ¶
func (r *Responder) Binary(w http.ResponseWriter, status int, in io.Reader, outName string, inline bool) error
Binary serve data as Binary response. usage:
var reader io.Reader
reader, _ = os.Open("./README.md")
r.Binary(w, http.StatusOK, reader, "readme.md", true)
func (*Responder) Data ¶
func (r *Responder) Data(w http.ResponseWriter, status int, v interface{}, contentType string) error
Data is the generic function called by XML, JSON, Data, HTML, and can be called by custom implementations.
func (*Responder) Empty ¶
func (r *Responder) Empty(w http.ResponseWriter) error
Empty alias method of the NoContent()
func (*Responder) HTML ¶
func (r *Responder) HTML(w http.ResponseWriter, status int, template string, v interface{}, layout ...string) error
HTML render HTML template file to http.ResponseWriter
func (*Responder) HTMLString ¶
func (r *Responder) HTMLString(w http.ResponseWriter, status int, tplContent string, v interface{}) error
HTMLString render HTML template string to http.ResponseWriter
func (*Responder) JSON ¶
func (r *Responder) JSON(w http.ResponseWriter, status int, v interface{}) error
JSON serve string content as json response
func (*Responder) LoadTemplateFiles ¶
LoadTemplateFiles load template files. usage:
LoadTemplateFiles("path/file1.tpl", "path/file2.tpl")
func (*Responder) LoadTemplateGlob ¶
LoadTemplateGlob load templates by glob usage:
LoadTemplateGlob("views/*")
LoadTemplateGlob("views/**/*")
func (*Responder) NoContent ¶
func (r *Responder) NoContent(w http.ResponseWriter) error
NoContent serve success but no content response