A content negotiation package for web applications in Go
Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
acceptheader.go
acceptheader_test.go
csvprocessor.go
csvprocessor_test.go
jsonprocessor.go
jsonprocessor_test.go
negotiate.go
negotiate_test.go
responseprocessor.go
txtprocessor.go
txtprocessor_test.go
weightedvalue.go
xmlprocessor.go
xmlprocessor_test.go

README.md

Negotiator GoDoc Build Status

This is a libary that handles content negotiation in web applications written in Go.

Usage

Simple

To return JSON/XML out of the box simple put this in your route handler:

func getUser(w http.ResponseWriter, req *http.Request) {
    user := &User{"Joe","Bloggs"}
    if err := negotiator.Negotiate(w, req, user); err != nil {
      http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

Custom

To add your own negotiator, for example you want to write a PDF with your model, do the following:

  1. Create a type that conforms to the ResponseProcessor interface

  2. Call negotiator.New(responseProcessors ...ResponseProcessor) and pass in a your custom processor. When your request handler calls negotiator.Negotiate(w,req,model,errorHandler) it will render a PDF if your Accept header defined it wanted a PDF response.