Gin框架无法从Postman获取数据,下面是一个demo:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
type CreateRequest struct {
Username string `json:"username"`
Phone string `json:"phone"`
Password string `json:"password"`
}
func Signup(c *gin.Context) {
var r CreateRequest
if err := c.Bind(&r); err != nil {
fmt.Println(err)
}
fmt.Println("this is debug info:start")
fmt.Println(r.Username)
fmt.Println("this is debug info:end")
}
func main() {
r := gin.Default()
r.POST("/signup", Signup)
r.Run() // listen and serve on 0.0.0.0:8080
}
postman 发送请求:
调试信息:
问题出在哪里?
我该怎么办?
最佳答案
根据您的屏幕,您似乎正在发送 x-www-form-urlencoded
请求,在这种情况下,您的 中必须有
结构:form
标签>CreateRequest
type CreateRequest struct {
Username string `json:"username" form:"username"`
Phone string `json:"phone" form:"phone"`
Password string `json:"password" form:"password"`
}
关于go - Gin 框架无法从 Postman 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53705933/