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