mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
improve parameter handling for ghttp.Requset
This commit is contained in:
@ -17,17 +17,17 @@ import (
|
||||
// It returns nil if <def> is not passed.
|
||||
//
|
||||
// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote
|
||||
// in order of priority: form < body.
|
||||
// in order of priority: form > body.
|
||||
func (r *Request) GetPost(key string, def ...interface{}) interface{} {
|
||||
r.ParseForm()
|
||||
r.ParseBody()
|
||||
if len(r.bodyMap) > 0 {
|
||||
if v, ok := r.bodyMap[key]; ok {
|
||||
if len(r.formMap) > 0 {
|
||||
if v, ok := r.formMap[key]; ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
if len(r.formMap) > 0 {
|
||||
if v, ok := r.formMap[key]; ok {
|
||||
r.ParseBody()
|
||||
if len(r.bodyMap) > 0 {
|
||||
if v, ok := r.bodyMap[key]; ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
@ -106,7 +106,7 @@ func (r *Request) GetPostInterfaces(key string, def ...interface{}) []interface{
|
||||
// the associated values are the default values if the client does not pass.
|
||||
//
|
||||
// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote
|
||||
// in order of priority: form < body.
|
||||
// in order of priority: form > body.
|
||||
func (r *Request) GetPostMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.ParseForm()
|
||||
r.ParseBody()
|
||||
@ -115,7 +115,7 @@ func (r *Request) GetPostMap(kvMap ...map[string]interface{}) map[string]interfa
|
||||
filter = true
|
||||
}
|
||||
m := make(map[string]interface{}, len(r.formMap)+len(r.bodyMap))
|
||||
for k, v := range r.formMap {
|
||||
for k, v := range r.bodyMap {
|
||||
if filter {
|
||||
if _, ok = kvMap[0][k]; !ok {
|
||||
continue
|
||||
@ -123,7 +123,7 @@ func (r *Request) GetPostMap(kvMap ...map[string]interface{}) map[string]interfa
|
||||
}
|
||||
m[k] = v
|
||||
}
|
||||
for k, v := range r.bodyMap {
|
||||
for k, v := range r.formMap {
|
||||
if filter {
|
||||
if _, ok = kvMap[0][k]; !ok {
|
||||
continue
|
||||
|
||||
@ -27,17 +27,17 @@ func (r *Request) SetQuery(key string, value interface{}) {
|
||||
// if <def> is not passed.
|
||||
//
|
||||
// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote
|
||||
// in order of priority: query < body.
|
||||
// in order of priority: query > body.
|
||||
func (r *Request) GetQuery(key string, def ...interface{}) interface{} {
|
||||
r.ParseQuery()
|
||||
r.ParseBody()
|
||||
if len(r.bodyMap) > 0 {
|
||||
if v, ok := r.bodyMap[key]; ok {
|
||||
if len(r.queryMap) > 0 {
|
||||
if v, ok := r.queryMap[key]; ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
if len(r.queryMap) > 0 {
|
||||
if v, ok := r.queryMap[key]; ok {
|
||||
r.ParseBody()
|
||||
if len(r.bodyMap) > 0 {
|
||||
if v, ok := r.bodyMap[key]; ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
@ -116,7 +116,7 @@ func (r *Request) GetQueryInterfaces(key string, def ...interface{}) []interface
|
||||
// the associated values are the default values if the client does not pass.
|
||||
//
|
||||
// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote
|
||||
// in order of priority: query < body.
|
||||
// in order of priority: query > body.
|
||||
func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.ParseQuery()
|
||||
r.ParseBody()
|
||||
@ -126,15 +126,6 @@ func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interf
|
||||
return kvMap[0]
|
||||
}
|
||||
m = make(map[string]interface{}, len(kvMap[0]))
|
||||
if len(r.queryMap) > 0 {
|
||||
for k, v := range kvMap[0] {
|
||||
if postValue, ok := r.queryMap[k]; ok {
|
||||
m[k] = postValue
|
||||
} else {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(r.bodyMap) > 0 {
|
||||
for k, v := range kvMap[0] {
|
||||
if postValue, ok := r.bodyMap[k]; ok {
|
||||
@ -144,12 +135,21 @@ func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interf
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(r.queryMap) > 0 {
|
||||
for k, v := range kvMap[0] {
|
||||
if postValue, ok := r.queryMap[k]; ok {
|
||||
m[k] = postValue
|
||||
} else {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m = make(map[string]interface{}, len(r.queryMap)+len(r.bodyMap))
|
||||
for k, v := range r.queryMap {
|
||||
for k, v := range r.bodyMap {
|
||||
m[k] = v
|
||||
}
|
||||
for k, v := range r.bodyMap {
|
||||
for k, v := range r.queryMap {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
@ -436,7 +436,7 @@ func Test_Params_Priority(t *testing.T) {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
gtest.Assert(client.GetContent("/query?a=1", "a=100"), "100")
|
||||
gtest.Assert(client.GetContent("/query?a=1", "a=100"), "1")
|
||||
gtest.Assert(client.PostContent("/post?a=1", "a=100"), "100")
|
||||
gtest.Assert(client.PostContent("/form?a=1", "a=100"), "100")
|
||||
gtest.Assert(client.PutContent("/form?a=1", "a=100"), "100")
|
||||
|
||||
Reference in New Issue
Block a user