go - Gin 框架无法从 Postman 获取数据

标签 go go-gin

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 发送请求:

Postman screenshot

调试信息:

debugger screenshot

问题出在哪里?
我该怎么办?

最佳答案

根据您的屏幕,您似乎正在发送 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/

相关文章:

reactjs - axios.post请求获取404

gorm exec 使用原始 sql 从插入事务返回 ID

unit-testing - 如何发送POST请求的multipart/form-data字段作为数组进行单元测试?

go - Gin 错误 : listen tcp: lookup addr on : no such host

ubuntu - 如何在 Ubuntu Precise 中安装当前版本的 Go

json - 在 golang 中像请求一样发送 curl

go - metadata.FromOutgoingContext 和 metadata.FromIncomingContext 有什么区别?

go - 是否有从 ios 日志中出现的字符的术语,如 `\M-C\M-6` 或 `\134`

docker - 如何处理 make missing from alpine docker image?

go - 无法从 Postman 中的 Get Url 获取对象 ID