使用gin接受post的json數據

第一種
func Login(c *gin.Context) {
    json := make(map[string]interface{}) //注意該結構接受的內容
    c.BindJSON(&json)
    log.Printf("%v",&json)
    c.JSON(http.StatusOK, gin.H{
        "name": json["name"],
        "password": json["password"],
    })
}
1
2
3
4
5
6
7
8
9
第二種
type User struct {
    Name string `json:"name"`
    Password int64 `json:"password"`
}
func Login(c *gin.Context) {
    json := User{}

    c.BindJSON(&json)

    log.Printf("%v",&json)
    c.JSON(http.StatusOK, gin.H{
        "name": json.Name,
        "password": json.Password,

    })
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章