Skip to content

Commit 39ea2b5

Browse files
committed
New example for the JSONP middleware
1 parent fd43529 commit 39ea2b5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [API and static files](#api-and-static-files)
2525
- [GORM](#gorm)
2626
- [CORS](#cors)
27+
- [JSONP](#jsonp)
2728
- [Basic Auth](#basic-auth)
2829
- [Status](#status)
2930
- [Status Auth](#status-auth)
@@ -680,6 +681,54 @@ func GetAllCountries(w rest.ResponseWriter, r *rest.Request) {
680681

681682
```
682683

684+
#### JSONP
685+
686+
Demonstrate how to use the JSONP middleware.
687+
688+
The curl demo:
689+
``` sh
690+
curl -i http://127.0.0.1:8080/message
691+
curl -i http://127.0.0.1:8080/message?cb=parseResponse
692+
```
693+
694+
695+
Go code:
696+
``` go
697+
package main
698+
699+
import (
700+
"github.com/ant0ine/go-json-rest/rest"
701+
"log"
702+
"net/http"
703+
)
704+
705+
type Message struct {
706+
Body string
707+
}
708+
709+
func main() {
710+
handler := rest.ResourceHandler{
711+
PreRoutingMiddlewares: []rest.Middleware{
712+
&rest.JsonpMiddleware{
713+
CallbackNameKey: "cb",
714+
},
715+
},
716+
}
717+
err := handler.SetRoutes(
718+
&rest.Route{"GET", "/message", func(w rest.ResponseWriter, req *rest.Request) {
719+
w.WriteJson(&Message{
720+
Body: "Hello World!",
721+
})
722+
}},
723+
)
724+
if err != nil {
725+
log.Fatal(err)
726+
}
727+
log.Fatal(http.ListenAndServe(":8080", &handler))
728+
}
729+
730+
```
731+
683732
#### Basic Auth
684733

685734
Demonstrate how to setup AuthBasicMiddleware as a pre-routing middleware.

0 commit comments

Comments
 (0)