本文整理匯總了Golang中github.com/vishaltelangre/cowboy/Godeps/_workspace/src/github.com/gin-gonic/gin.Context.JSON方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.JSON方法的具體用法?Golang Context.JSON怎麽用?Golang Context.JSON使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/vishaltelangre/cowboy/Godeps/_workspace/src/github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.JSON方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Handler
// Handler is a route handler for '/movie.:format' route
func Handler(c *gin.Context) {
requestType := c.Param("format")
term := c.Request.PostFormValue("text")
log.Printf("Movie Query: %s", term)
movie, err := lookupMovie(term)
switch requestType {
case "json":
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"Response": err.Error()})
} else {
c.IndentedJSON(http.StatusOK, movie)
}
case "slack":
text, err := formatSlackResp(movie)
if err != nil {
c.String(http.StatusNotFound, "Not Found")
}
c.String(http.StatusOK, text)
default:
c.JSON(http.StatusUnsupportedMediaType, nil)
}
}
示例2: Handler
// Handler is a route handler for '/movie.:format' route
func Handler(c *gin.Context) {
requestType := c.Param("format")
text := c.Request.PostFormValue("text")
operator, circle := parsedReqText(text)
log.Printf("Recharge Query:: Circle := %s, Operator := %s", circle, operator)
var list []Plan
list, err := findPlans(circle, operator)
switch requestType {
case "json":
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"Response": err.Error()})
} else {
c.IndentedJSON(http.StatusOK, list)
}
case "slack":
text, err := formatSlackResp(list)
if err != nil {
c.String(http.StatusNotFound, "Not Found")
}
c.String(http.StatusOK, text)
default:
c.JSON(http.StatusUnsupportedMediaType, nil)
}
}
示例3: PostsHandler
// PostsHandler is a route handler for '/producthunt/posts.:format' route
func PostsHandler(c *gin.Context) {
requestType := c.Param("format")
daysAgo, err := strconv.Atoi(c.Request.PostFormValue("text"))
if err != nil {
daysAgo = 0
}
log.Printf("Days Ago: %d", daysAgo)
posts, err := getPosts(daysAgo)
switch requestType {
case "json":
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"Response": err.Error()})
} else {
c.IndentedJSON(http.StatusOK, posts)
}
case "slack":
text, err := formatPostsSlackResp(posts)
if err != nil {
c.String(http.StatusNotFound, "Not Found")
}
c.String(http.StatusOK, text)
default:
c.JSON(http.StatusUnsupportedMediaType, nil)
}
}
示例4: Handler
// Handler is a route handler for '/excuse.:format' route
func Handler(c *gin.Context) {
requestType := c.Param("format")
excuse, err := getAnExcuse()
switch requestType {
case "json":
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"Response": err.Error()})
} else {
c.IndentedJSON(http.StatusOK, excuse)
}
case "slack":
if err != nil {
c.String(http.StatusNotFound, "> I am not feeling well\n")
}
c.String(http.StatusOK, "\n> "+excuse.Text+"\n")
default:
c.String(http.StatusUnsupportedMediaType, "")
}
}
示例5: roomPOST
func roomPOST(c *gin.Context) {
roomid := c.Param("roomid")
nick := c.Query("nick")
message := c.PostForm("message")
message = strings.TrimSpace(message)
validMessage := len(message) > 1 && len(message) < 200
validNick := len(nick) > 1 && len(nick) < 14
if !validMessage || !validNick {
c.JSON(400, gin.H{
"status": "failed",
"error": "the message or nickname is too long",
})
return
}
post := gin.H{
"nick": html.EscapeString(nick),
"message": html.EscapeString(message),
}
messages.Add("inbound", 1)
room(roomid).Submit(post)
c.JSON(200, post)
}