|
| 1 | +# JSONP Go http middleware |
| 2 | + |
| 3 | +JSONP is a common technique used to communicate with a JSON-serving Web Service with a |
| 4 | +Web browser over cross-domains, in place of a XHR request. There is a lot written about |
| 5 | +JSONP out there, but the tl;dr on it is a Javascript http client requesting JSONP |
| 6 | +will write a `<script>` tag to the head of a page, with the `src` to an API endpoint, |
| 7 | +with the addition of a `callback` (or `jsonp`) query parameter that represents a |
| 8 | +randomly-named listener function that will parse the request when it comes back from |
| 9 | +the server. |
| 10 | + |
| 11 | +This middleware will work with anything that supports standard `http.Handler`. The code |
| 12 | +is small, so go read it, but it just buffers the response from the rest of the chain, |
| 13 | +and if its a JSON request with a callback, then it will wrap the response in the callback |
| 14 | +function before writing it to the actual response writer. |
| 15 | + |
| 16 | +Any feedback is welcome and appreciated! |
| 17 | + |
| 18 | +Written by [@pkieltyka](https://github.com/pkieltyka) |
| 19 | + |
| 20 | +## Example |
| 21 | + |
| 22 | +```go |
| 23 | +// JSONP example using Goji framework.. but anything that accepts |
| 24 | +// a http.Handler middleware chain will work |
| 25 | +package main |
| 26 | + |
| 27 | +import ( |
| 28 | + "log" |
| 29 | + "net/http" |
| 30 | + |
| 31 | + "github.com/goware/jsonp" |
| 32 | + "github.com/unrolled/render" |
| 33 | + "github.com/zenazn/goji/web" |
| 34 | + "github.com/zenazn/goji/web/middleware" |
| 35 | +) |
| 36 | + |
| 37 | +func main() { |
| 38 | + mux := web.New() |
| 39 | + render := render.New(render.Options{}) |
| 40 | + |
| 41 | + mux.Use(middleware.Logger) |
| 42 | + mux.Use(jsonp.Handler) |
| 43 | + |
| 44 | + mux.Get("/", func(w http.ResponseWriter, r *http.Request) { |
| 45 | + data := &SomeObj{"superman"} |
| 46 | + render.JSON(w, 200, data) |
| 47 | + }) |
| 48 | + |
| 49 | + err := http.ListenAndServe(":4444", mux) |
| 50 | + if err != nil { |
| 51 | + log.Fatal(err) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +type SomeObj struct { |
| 56 | + Name string `json:"name"` |
| 57 | +} |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +*Output:* |
| 62 | +``` |
| 63 | +curl -v "http://localhost:4444/" |
| 64 | +> GET / HTTP/1.1 |
| 65 | +> User-Agent: curl/7.37.1 |
| 66 | +> Host: localhost:4444 |
| 67 | +> Accept: */* |
| 68 | +> |
| 69 | +< HTTP/1.1 200 OK |
| 70 | +< Content-Type: application/json; charset=UTF-8 |
| 71 | +< Date: Sat, 08 Nov 2014 16:05:04 GMT |
| 72 | +< Content-Length: 19 |
| 73 | +< |
| 74 | +* Connection #0 to host localhost left intact |
| 75 | +{"name":"superman"} |
| 76 | +``` |
| 77 | + |
| 78 | +``` |
| 79 | +curl -v "http://localhost:4444/?callback=X" |
| 80 | +> GET /?callback=X HTTP/1.1 |
| 81 | +> User-Agent: curl/7.37.1 |
| 82 | +> Host: localhost:4444 |
| 83 | +> Accept: */* |
| 84 | +> |
| 85 | +< HTTP/1.1 200 OK |
| 86 | +< Content-Length: 22 |
| 87 | +< Content-Type: application/javascript |
| 88 | +< Date: Sat, 08 Nov 2014 16:05:23 GMT |
| 89 | +< |
| 90 | +* Connection #0 to host localhost left intact |
| 91 | +X({"name":"superman"}) |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +## Also |
| 96 | + |
| 97 | +Since JSONP must always respond with a 200, as thats what the browser `<script>` |
| 98 | +tag expects, a nice pattern that is also used in the GitHub API is to put the HTTP |
| 99 | +response headers in a `"meta"` hash, and the HTTP response body in `"data"`. Like so.. |
| 100 | + |
| 101 | +```json |
| 102 | +JsonpCallbackFn_abc123etc({ |
| 103 | + "meta": { |
| 104 | + "Status": 200, |
| 105 | + "Content-Type": "application/json", |
| 106 | + "Content-Length": "19", |
| 107 | + "etc": "etc" |
| 108 | + }, |
| 109 | + "data": { "name": "yummy" } |
| 110 | +}) |
| 111 | +``` |
0 commit comments