我有以下 Gin 中间件:
func CheckAppId(appC *core.Context) gin.HandlerFunc {
return func(c *gin.Context) {
//get Basic Auth credentials
appId, token, _ := c.Request.BasicAuth()
if appId == "" {
c.JSON(http.StatusOK, gin.H{"code": "MISSING_APP_ID", "message": "Your request is missing an application id"})
return //this is being ignored???
}
c.Next() //this still gets hit
}
}
但是如果appId == ""
JSON 被返回c.Next()
并被执行。这是预期的行为吗?
编辑 我认为问题已被出售,但似乎正在发生同样的事情。我现在有:
func CheckAppId(appC *core.Context) gin.HandlerFunc {
return func(c *gin.Context) {
//get Basic Auth credentials
appId, token, _ := c.Request.BasicAuth()
if appId == "" {
//I'm getting this JSON in the response
c.JSON(http.StatusOK, gin.H{"code": "MISSING_APP_ID", "message": "Your request is missing an application id"})
c.Abort()
}
//find app_id in database
app := core.App{}
err := appC.Database.C("apps").Find(bson.M{"id" : appId}).One(&app)
if err != nil { //no app found
//and I'm getting this JSON in the response
c.JSON(http.StatusOK, gin.H{"code": "INVALID_APP_ID", "message": "The application id provided could not be found"})
c.Abort()
}
c.Next()
}
}
在对 API 的调用中,我同时收到“MISSING_APP_ID”Json 和“INVALID_APP_ID”Json