Skip to content

Instantly share code, notes, and snippets.

@mrvdot
mrvdot / jsonphandler.go
Last active December 21, 2015 10:58
If you've ever wanted to handle JSONP callbacks within a Go powered API, here's a simple wrapper that you can use to intercept the callback query variable and properly wrap the response. It's designed to take an entire http.Handler, so works particularly well when using a router such as mux from gorilla, however can work with any handler.
import (
"io"
"net/http"
)
//With HandlerFunc
func main () {
http.Handle("/", JsonpHandler(http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
w.Write("Hello World")
})))