为什么 post 请求在 gin golang 中不起作用?

Why is the post request not working in gin golang?

此代码不起作用,响应将是空的,像这样{"test":""}

    func main() {
            router := gin.Default()

            router.POST("/test", f

unc(c *gin.Context) {
            test := c.Query("test")
            c.JSON(200, gin.H{
                "test": test,
            })
        })
        router.Run()
    }

更新: 我通过 struct:

找到了简单的解决方案
func test(c *gin.Context) {
    test := struct {
        Test   string `json:"test"`
        Test2 string `json:"test2"`
    }{}
    c.BindJSON(&test)

    c.JSON(200, gin.H{
        "test1":  test.Test,
        "test2": test.Test2,
    })
}

您正在将数据作为正文发送,您应该将正文绑定到一个变量以访问它。

type Data struct {
   test string
}
// ...

router.POST("/test", func(c *gin.Context) {
   var data Data        
   c.BindJSON(&data)

   c.JSON(200, gin.H{ 
      "test": data.test,
   })
})
func test(c *gin.Context) {
    test := struct {
        Test   string `json:"test"`
        Test2 string `json:"test2"`
    }{}
    c.BindJSON(&test)

    c.JSON(200, gin.H{
        "test1":  test.Test,
        "test2": test.Test2,
    })
}