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})
|
||||
}
|
||||
})
|
||||
// Server starten auf Port 8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<body>
|
||||
<a href='/ping'>Ping</a><br />
|
||||
<form action="/echo" method="post">
|
||||
<input type="text" name="message" />
|
||||
<input type="submit" value="und ab damit" />
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user