respond

package module
v0.0.0-...-0a032df Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2018 License: MIT Imports: 4 Imported by: 0

README

Respond GoDoc Build Status Go Report Card Coverage

Go package for easily responding to HTTP requests with common response types, setting the appropriate Content-Type and Status headers as needed.

  • HTML
  • Template (using html/template)
  • Text
  • JSON (using encoding/json)
  • XML (using encoding/xml)

Usage

Respond can be used with pretty much any web framework. As long as you have access to a http.ResponseWriter, you are good to go.

import "github.com/dannyvankooten/respond"
// this sets Content-Type (incl. charset) and Status header before writing the response body.
func myHandler(w http.ResponseWriter, r *http.Request) {
   respond.HTML(w, http.StatusOK, []byte("Hello world!"))
}

// if you just want to set the Content-Type and Status header, omit the last parameter
func myHandler(w http.ResponseWriter, r *http.Request) {
   respond.HTML(w, http.StatusOK)
}

// html/template.*Template
func myHandler(w http.ResponseWriter, r *http.Request) {
	tmpl := template.Must(template.New("").Parse("Hello {{.}}"))
	respond.Template(w, http.StatusOK, tmpl, "world")
}

// JSON
func myHandler(w http.ResponseWriter, r *http.Request) {
   respond.JSON(w, http.StatusOK, map[string]string{"foo": "bar"})
}

// Text
func myHandler(w http.ResponseWriter, r *http.Request) {
   respond.Text(w, http.StatusOK, []byte("Hello world!"))
}

// XML
func myHandler(w http.ResponseWriter, r *http.Request) {
   respond.XML(w, http.StatusOK, map[string]string{"foo": "bar"})
}

// XML with struct
func myHandler(w http.ResponseWriter, r *http.Request) {
   respond.XML(w, http.StatusOK, &myType{ Name: "John Doe" })
}

Respond defaults to UTF-8 as its charset. To override it, set the package global named Charset.

respond.Charset = "UTF-16"

License

MIT licensed.

Documentation

Overview

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

View Source
const (
	// ContentType HTTP header name for defining the content type
	ContentType = "Content-Type"

	// ContentHTML HTTP header value for HTML data
	ContentHTML = "text/html"

	// ContentJSON HTTP header value for JSON data
	ContentJSON = "application/json"

	// ContentText header value for Text data.
	ContentText = "text/plain"

	// ContentXML header value for XML data.
	ContentXML = "text/xml"
)

Variables

View Source
var Charset = "UTF-8"

Functions

func HTML

func HTML(w http.ResponseWriter, statusCode int, data ...[]byte) error

HTML sets a Content-Type: text/html header with charset and writes the byte data to the stream

func JSON

func JSON(w http.ResponseWriter, statusCode int, data ...interface{}) error

JSON renders the data as a JSON HTTP response to the ResponseWriter

func Template

func Template(w http.ResponseWriter, statusCode int, tmpl *template.Template, data ...interface{}) error

Template executes the template and writes to the responsewriter

func Text

func Text(w http.ResponseWriter, statusCode int, data ...[]byte) error

Text writes the data as a JSON HTTP response to the ResponseWriter

func XML

func XML(w http.ResponseWriter, statusCode int, data ...interface{}) error

XML writes the data as a XML HTTP response to the ResponseWriter

Types

This section is empty.

Jump to

Keyboard shortcuts

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