tapocol / accept

Go Martini Middleware handler converts Accept header to standardized format.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accept (Martini Middleware)

Using regexp patterns in Go, this project will match the "Accept" HTTP Header to a Format string. The Format string will be mapped during the Martini Middleware.

Usage

Every Request:

m.Use(accept.Accept)
m.Get("/", func(f *Format) string {
  switch *f {
  case accept.JSON:
    return `{"Hello": "JSON"}`
  default:
    return "Hello HTML"
  }
})

Route-specific:

m.Get("/", accept.Accept, func(f *Format) string {
  switch *f {
  case accept.JSON:
    return `{"Hello": "JSON"}`
  default:
    return "Hello HTML"
  }
})

Setup

Download:

$ go get github.com/craigjackson/accept

Change the default format (inititalizes to accept.HTML):

accept.DefaultFormat = accept.JSON

Add addition formats to attempt to match (initializes to just use HTML and JSON):

accept.Formats[XML] = accept.XML_PATTERN

About

Go Martini Middleware handler converts Accept header to standardized format.


Languages

Language:Go 100.0%