in my case, I want to use anothor param instead of callback
my url: http://example.com?id=1&cb=callback1
but I found this in the source code:
// JSONP serializes the given struct as JSON into the response body.
// It adds padding to response body to request data from a server residing in a different domain than the client.
// It also sets the Content-Type as "application/javascript".
func (c *Context) JSONP(code int, obj interface{}) {
callback := c.DefaultQuery("callback", "")
if callback == "" {
c.Render(code, render.JSON{Data: obj})
return
}
c.Render(code, render.JsonpJSON{Callback: callback, Data: obj})
}
how can I use the param cb instead of callback
You can use middleware for gin. Modifying the query before it is parsed.
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/url"
)
func main() {
r := gin.Default()
r.Use(func(context *gin.Context) {
req := context.Request
urlCopy, _ := req.URL.Parse(req.RequestURI)
if cb := urlCopy.Query().Get("cb"); cb != "" {
req.URL.RawQuery += fmt.Sprintf("&callback=%s", url.QueryEscape(cb))
}
})
r.GET("/ping", func(c *gin.Context) {
c.JSONP(400, 1)
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}