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

Unable to get Object Id from Get Url in Postman

我的 url 似乎不匹配,因为它显示了 404 错误,我尝试更改邮递员的 url 和我的代码 too.Also 尝试使用对象 ID转换以查看 404 是否由此引起。

func main() {
    r := gin.Default()
    r.GET("/get-custone/:_id", getDetailone)
    r.Run()
} 
func getDetailone(c *gin.Context) {
    session := connect()
    defer session.Close()
    col := session.DB("test").C("cust")
    var results Person
    idstring:=c.Param("_id")
    oid:=bson.ObjectId(idstring)
    err := col.Find(bson.M{"_id":oid}).One(&results)
    if err != nil {
        panic(err)
    }
    c.JSON(200, gin.H{
        "message": "success",
    })
}

这是 Postman 的截图

在 Postman 中,您试图将 _id 作为查询字符串传递,而您正在等待代码中的路径参数。

你想做的是:

curl -X GET http://localhost:8080/get-custone/5b7d...

如果你更喜欢使用查询字符串参数,你应该做类似的事情(我没有测试代码):

func main() {
    r := gin.Default()
    r.GET("/get-custone", getDetailone)
    r.Run()
} 
func getDetailone(c *gin.Context) {
    session := connect()
    defer session.Close()
    col := session.DB("test").C("cust")
    var results Person
    idstring:= c.Query("_id")
    oid:=bson.ObjectId(idstring)
    err := col.Find(bson.M{"_id":oid}).One(&results)
    if err != nil {
        panic(err)
    }
    c.JSON(200, gin.H{
        "message": "success",
    })
}