html form example

This commit is contained in:
jotty
2026-05-23 17:11:30 +02:00
parent c257151fbb
commit 09146887b2
2 changed files with 23 additions and 7 deletions
+11 -7
View File
@@ -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")
}
+12
View File
@@ -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>