Library provides set of middlewares for setting Content-Type
header.
With a properly configured Go toolchain:
go get -u github.com/furdarius/contenttype
package main
import (
"fmt"
"net/http"
"github.com/furdarius/contenttype"
)
func main() {
// Preferred router initialization
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Welcome to the home page!")
})
handler := contenttype.Middleware(mux, "application/json")
// Also you can use predefined contenttype middlewares.
// Available middlewares: JSON, HTML, XML, PDF, Text, OctetStream
//
// You can change middleware above with:
// handler := contenttype.JSON(mux)
http.ListenAndServe(":3000", handler)
}
JSON middleware set header Content-Type: application/json
h := contenttype.JSON(mux)
HTML middleware set header Content-Type: text/html
h := contenttype.HTML(mux)
XML middleware set header Content-Type: application/xml
h := contenttype.XML(mux)
PDF middleware set header Content-Type: application/pdf
h := contenttype.PDF(mux)
Text middleware set header Content-Type: text/plain
h := contenttype.Text(mux)
OctetStream middleware set header Content-Type: application/octet-stream
h := contenttype.OctetStream(mux)