我想创建一个函数来处理任何形式的表单。 我希望它能够处理任何类型的数据类型。 我正在尝试使用界面来完成此任务。
type Person struct {
name string
lastName string
}
func HTMLForm(c *gin.Context) {
var f Person
c.ShouldBind(&f)
c.JSON(http.StatusOK, f)
}
// with this function i get the correct info
// output: {"name":"john","lastName":"snow"}
func HTMLForm(c *gin.Context) {
var f interface{}
c.ShouldBind(&f)
c.JSON(http.StatusOK, f)
}
// when i use the interface to make it usefull for any type of that
// i get null
// output: null
func HTMLForm(c *gin.Context) {
var f interface{}
ShouldBindJSON(f)
c.JSON(http.StatusOK, f)
}
// output: null
我想通过该接口获得与“ Person”数据类型相同的输出。
// Another example of how i am using f
type Login struct {
User string
Password string
}
func main() {
router := gin.Default()
router.POST("/loginForm", func(c *gin.Context) {
var f interface{}
if err := c.ShouldBind(&f); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, f)
})
// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
}
// output: null
///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// //////////// 更新
我想尝试更好地解释我的问题。 也许这次更新更加清晰。
// Golang code
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Binding from JSON
type Login struct {
User string `form:"user" json:"user" xml:"user" binding:"required"`
Password string `form:"password" json:"password" xml:"password" binding:"required"`
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/login", GetLogin)
router.POST("/loginJSON", PostJSONForm)
router.POST("/loginXML", PostXMLForm)
router.POST("/loginForm", PostHTMLForm)
/*
sudo lsof -n -i :8080
kill -9 <PID>
*/
router.Run(":8080")
}
func GetLogin(c *gin.Context) {
c.HTML(http.StatusOK, "login.tmpl", nil)
}
// Example for binding JSON ({"user": "manu", "password": "123"})
// curl -v -X POST http://localhost:8080/loginJSON -H 'content-type: application/json' '{ "user": "manu", "password"="123" }'
func PostJSONForm(c *gin.Context) {
//var json Login
var json interface{}
//var form map[string]interface{}
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
/*
if json.User != "manu" || json.Password != "123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
*/
c.JSON(http.StatusOK, "json")
c.JSON(http.StatusOK, json)
}
// Example for binding XML (
// <?xml version="1.0" encoding="UTF-8"?>
// <root>
// <user>user</user>
// <password>123</password>
// </root>)
// curl -v -X POST http://localhost:8080/loginXML -H 'content-type: application/json' -d '{ "user": "manu", "password"="123" }'
func PostXMLForm(c *gin.Context) {
//var xml Login
var xml interface{}
//var form map[string]interface{}
if err := c.ShouldBindXML(&xml); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
/*
if xml.User != "manu" || xml.Password != "123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
*/
c.JSON(http.StatusOK, "xml")
c.JSON(http.StatusOK, xml)
}
// Example for binding a HTML form (user=manu&password=123)
// curl -v -X POST http://localhost:8080/loginForm -H 'content-type: application/json' -d '{ "user": "manu", "password":"123" }'
func PostHTMLForm(c *gin.Context) {
//var form Login
var form interface{}
//var form map[string]interface{}
// This will infer what binder to use depending on the content-type header.
if err := c.ShouldBind(&form); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
/*
if form.User != "manu" || form.Password != "123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
*/
c.JSON(http.StatusOK, "html")
c.JSON(http.StatusOK, form)
}
//Template
<h1>Login</h1>
<form action="/loginForm" method="POST">
<label>user:</label><br>
<input type="text" name="user"><br>
<label>password:</label><br>
<input type="text" name="password"><br>
<input type="submit">
</form>
我尝试了所有这些不同的变体。只是一件作品,我在下面进一步解释。
如果我使用“ var form Login”而不是“ interface {}中的var”,则效果很好。但是我需要它能够与任何数据类型一起使用,因此我需要它与interface {}一起使用。
仅通过interface {}之一尝试,我就获得了“成功”输出:
$ curl -X POST http://localhost:8080/loginForm -H'内容类型:application / json'-d'{“ user”:“ manu”,“ password”:“ 123”}'
output:“ html” {“ password”:“ 123”,“ user”:“ manu”}
输出:“ html”为空
$ curl -X POST http://localhost:8080/loginForm -H'内容类型:application / json'-d'{“ user”:“ manu”,“ password”:“ 123”}'
输出:“ html” {“ user”:“ manu”,“ password”:“ 123”}
这是我到目前为止可以获得的所有信息。
答案 0 :(得分:0)
您是否尝试使用 ShouldBindJSON ?因为它必须知道您想成为哪种类型
ShouldBind检查Content-Type以自动选择绑定引擎,根据“ Content-Type”标头使用不同的绑定:
来自gin的源代码:
// ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).
func (c *Context) ShouldBindJSON(obj interface{}) error {
return c.ShouldBindWith(obj, binding.JSON)
}
更新: 就您而言,它对我有用吗?
router := gin.Default()
router.POST("/loginForm", func(c *gin.Context) {
var f interface{}
if err := c.ShouldBindJSON(&f); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
log.Print(f)
c.JSON(http.StatusOK, f)
})
// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
登录示例
curl -X POST -H "Content-Type: application/json" \
--data '{"username":"xyz","password":"xyz"}' localhost:8080/loginForm
结果
[GIN-debug] POST /loginForm --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
map[password:xyz username:xyz]
[GIN] 2019/04/23 - 10:07:00 | 200 | 239.317µs | ::1 | POST /loginForm
/////////////////////////////////
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Binding from JSON
type Login struct {
User string `form:"user" json:"user" xml:"user" binding:"required"`
Password string `form:"password" json:"password" xml:"password" binding:"required"`
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/login", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.tmpl", nil)
})
// Example for binding a HTML form (user=manu&password=123)
router.POST("/loginForm", func(c *gin.Context) {
var form interface{}
// This will infer what binder to use depending on the content-type header.
if err := c.ShouldBind(&form); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, form)
})
// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
}
//模板
<h1>Login</h1>
<form action="/loginForm" method="POST">
<label>user:</label><br>
<input type="text" name="user"><br>
<label>password:</label><br>
<input type="text" name="password"><br>
<input type="submit">
</form>
///当我使用de HTML时,当我尝试curl命令时,它会为空