html form example
This commit is contained in:
@@ -7,24 +7,28 @@ import (
|
||||
)
|
||||
|
||||
type EchoRequest struct {
|
||||
Message string `json:"message" binding:"required"`
|
||||
Message string `form:"message" json:"message" binding:"required"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
// GET /ping
|
||||
r.LoadHTMLGlob("templates/*")
|
||||
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
||||
})
|
||||
// POST /echo
|
||||
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "index.html", nil)
|
||||
})
|
||||
|
||||
r.POST("/echo", func(c *gin.Context) {
|
||||
var req EchoRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"echo": req.Message})
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"echo": req.Message})
|
||||
})
|
||||
// Server starten auf Port 8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user