fix issue in ghttp.Request.GetRequest*

This commit is contained in:
John
2019-12-05 10:44:31 +08:00
parent 64bb72842e
commit f775479c3f
3 changed files with 8 additions and 35 deletions

View File

@ -1,42 +1,15 @@
package main
import (
"net/http"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func MiddlewareAuth(r *ghttp.Request) {
token := r.Get("token")
if token == "123456" {
r.Middleware.Next()
} else {
r.Response.WriteStatus(http.StatusForbidden)
}
}
func MiddlewareCORS(r *ghttp.Request) {
r.Response.CORSDefault()
r.Middleware.Next()
}
func MiddlewareLog(r *ghttp.Request) {
r.Middleware.Next()
g.Log().Println(r.Response.Status, r.URL.Path, r.GetError().Error())
}
func main() {
s := g.Server()
s.SetConfigWithMap(g.Map{
"AccessLogEnabled": false,
"ErrorLogEnabled": false,
})
s.BindMiddlewareDefault(MiddlewareLog)
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
group.Middleware(MiddlewareAuth, MiddlewareCORS)
group.ALL("/user/list", func(r *ghttp.Request) {
panic("啊!我出错了!")
group.ALL("/test", func(r *ghttp.Request) {
r.Response.Write(r.GetRequest("nickname"))
})
})
s.SetPort(8199)

View File

@ -1,4 +1,4 @@
# `v1.9.3`
# `v1.9.3` (2019-09-24)
该版本实际为`v2.0`的大版本发布,为避免`go module`机制严格要求`v2`版本以上需要修改`import`并加上`v2`后缀,因此使用了`v1.9`版本进行发布。

View File

@ -19,18 +19,18 @@ import (
// GetRequest is one of the most commonly used functions for retrieving parameters.
//
// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote
// in order of priority: router < query < form < body < custom.
// in order of priority: router < query < body < form < custom.
func (r *Request) GetRequest(key string, def ...interface{}) interface{} {
value := r.GetParam(key)
if value == nil {
value = r.GetForm(key)
}
if value == nil {
r.ParseBody()
if len(r.bodyMap) > 0 {
value = r.bodyMap[key]
}
}
if value == nil {
value = r.GetForm(key)
}
if value == nil {
value = r.GetQuery(key)
}
@ -166,7 +166,7 @@ func (r *Request) GetRequestInterfaces(key string, def ...interface{}) []interfa
// GetRequestMap is one of the most commonly used functions for retrieving parameters.
//
// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote
// in order of priority: router < query < form < body < custom.
// in order of priority: router < query < body < form < custom.
func (r *Request) GetRequestMap(kvMap ...map[string]interface{}) map[string]interface{} {
r.ParseQuery()
r.ParseForm()