亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 post 請求在 gin golang 中不起作用?

為什么 post 請求在 gin golang 中不起作用?

Go
胡子哥哥 2023-03-29 15:29:46
這段代碼不起作用,響應將是空的,就像這樣{"test":""}。    func main() {            router := gin.Default()            router.POST("/test", func(c *gin.Context) {            test := c.Query("test")            c.JSON(200, gin.H{                "test": test,            })        })        router.Run()    }更新:我通過結構找到了簡單的解決方案: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,    })}
查看完整描述

2 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

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,

    })

}


查看完整回答
反對 回復 2023-03-29
?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

您將數據作為正文發送,您應該將正文綁定到一個變量以訪問它。


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,

   })

})


查看完整回答
反對 回復 2023-03-29
  • 2 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號