2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-07-29 22:01:29 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-07-29 22:01:29 +08:00
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/container/gvar"
|
2019-07-16 16:23:03 +08:00
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/util/gconv"
|
2018-07-29 22:01:29 +08:00
|
|
|
)
|
|
|
|
|
2019-11-29 22:02:19 +08:00
|
|
|
// SetQuery sets custom query value with key-value pair.
|
2019-09-18 23:20:45 +08:00
|
|
|
func (r *Request) SetQuery(key string, value interface{}) {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseQuery()
|
2019-11-29 22:02:19 +08:00
|
|
|
if r.queryMap == nil {
|
|
|
|
r.queryMap = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
r.queryMap[key] = value
|
2018-07-31 14:28:41 +08:00
|
|
|
}
|
|
|
|
|
2019-11-29 22:02:19 +08:00
|
|
|
// GetQuery retrieves and returns parameter with given name <key> from query string
|
2020-04-06 22:31:45 +08:00
|
|
|
// and request body. It returns <def> if <key> does not exist in the query and <def> is given,
|
|
|
|
// or else it returns nil.
|
2019-12-01 14:07:36 +08:00
|
|
|
//
|
2020-04-06 22:31:45 +08:00
|
|
|
// Note that if there're multiple parameters with the same name, the parameters are retrieved
|
|
|
|
// and overwrote in order of priority: query > body.
|
2019-09-18 23:20:45 +08:00
|
|
|
func (r *Request) GetQuery(key string, def ...interface{}) interface{} {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseQuery()
|
2019-12-01 21:41:29 +08:00
|
|
|
if len(r.queryMap) > 0 {
|
|
|
|
if v, ok := r.queryMap[key]; ok {
|
2019-11-29 22:02:19 +08:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseBody()
|
2019-12-01 21:41:29 +08:00
|
|
|
if len(r.bodyMap) > 0 {
|
|
|
|
if v, ok := r.bodyMap[key]; ok {
|
2019-11-29 22:02:19 +08:00
|
|
|
return v
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
if len(def) > 0 {
|
2019-09-18 23:20:45 +08:00
|
|
|
return def[0]
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:23:03 +08:00
|
|
|
func (r *Request) GetQueryVar(key string, def ...interface{}) *gvar.Var {
|
2019-09-18 23:20:45 +08:00
|
|
|
return gvar.New(r.GetQuery(key, def...))
|
2019-07-16 16:23:03 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryString(key string, def ...interface{}) string {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).String()
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryBool(key string, def ...interface{}) bool {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Bool()
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryInt(key string, def ...interface{}) int {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Int()
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-10-10 10:33:14 +08:00
|
|
|
func (r *Request) GetQueryInt32(key string, def ...interface{}) int32 {
|
|
|
|
return r.GetQueryVar(key, def...).Int32()
|
|
|
|
}
|
|
|
|
|
2019-10-10 10:40:38 +08:00
|
|
|
func (r *Request) GetQueryInt64(key string, def ...interface{}) int64 {
|
|
|
|
return r.GetQueryVar(key, def...).Int64()
|
2019-10-10 10:33:14 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryInts(key string, def ...interface{}) []int {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Ints()
|
2018-10-09 10:05:55 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryUint(key string, def ...interface{}) uint {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Uint()
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-10-10 10:33:14 +08:00
|
|
|
func (r *Request) GetQueryUint32(key string, def ...interface{}) uint32 {
|
|
|
|
return r.GetQueryVar(key, def...).Uint32()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetQueryUint64(key string, def ...interface{}) uint64 {
|
|
|
|
return r.GetQueryVar(key, def...).Uint64()
|
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryFloat32(key string, def ...interface{}) float32 {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Float32()
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryFloat64(key string, def ...interface{}) float64 {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Float64()
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryFloats(key string, def ...interface{}) []float64 {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Floats()
|
2018-10-09 10:05:55 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryArray(key string, def ...interface{}) []string {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Strings()
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryStrings(key string, def ...interface{}) []string {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Strings()
|
2018-10-09 10:05:55 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Request) GetQueryInterfaces(key string, def ...interface{}) []interface{} {
|
2019-09-18 23:20:45 +08:00
|
|
|
return r.GetQueryVar(key, def...).Interfaces()
|
2018-10-09 10:05:55 +08:00
|
|
|
}
|
|
|
|
|
2019-11-29 22:02:19 +08:00
|
|
|
// GetQueryMap retrieves and returns all parameters passed from client using HTTP GET method
|
|
|
|
// as map. The parameter <kvMap> specifies the keys retrieving from client parameters,
|
|
|
|
// the associated values are the default values if the client does not pass.
|
2019-12-01 14:07:36 +08:00
|
|
|
//
|
|
|
|
// Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote
|
2019-12-01 21:41:29 +08:00
|
|
|
// in order of priority: query > body.
|
2019-09-18 23:20:45 +08:00
|
|
|
func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseQuery()
|
|
|
|
r.parseBody()
|
2019-12-01 14:07:36 +08:00
|
|
|
var m map[string]interface{}
|
|
|
|
if len(kvMap) > 0 && kvMap[0] != nil {
|
2019-11-29 22:02:19 +08:00
|
|
|
if len(r.queryMap) == 0 && len(r.bodyMap) == 0 {
|
|
|
|
return kvMap[0]
|
|
|
|
}
|
2019-12-01 14:07:36 +08:00
|
|
|
m = make(map[string]interface{}, len(kvMap[0]))
|
2019-12-01 21:41:29 +08:00
|
|
|
if len(r.bodyMap) > 0 {
|
2019-11-29 22:02:19 +08:00
|
|
|
for k, v := range kvMap[0] {
|
2019-12-01 21:41:29 +08:00
|
|
|
if postValue, ok := r.bodyMap[k]; ok {
|
2019-11-29 22:02:19 +08:00
|
|
|
m[k] = postValue
|
|
|
|
} else {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 21:41:29 +08:00
|
|
|
if len(r.queryMap) > 0 {
|
2019-11-29 22:02:19 +08:00
|
|
|
for k, v := range kvMap[0] {
|
2019-12-01 21:41:29 +08:00
|
|
|
if postValue, ok := r.queryMap[k]; ok {
|
2019-11-29 22:02:19 +08:00
|
|
|
m[k] = postValue
|
|
|
|
} else {
|
|
|
|
m[k] = v
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-18 23:20:45 +08:00
|
|
|
} else {
|
2019-12-01 14:07:36 +08:00
|
|
|
m = make(map[string]interface{}, len(r.queryMap)+len(r.bodyMap))
|
2019-12-01 21:41:29 +08:00
|
|
|
for k, v := range r.bodyMap {
|
2019-11-29 22:02:19 +08:00
|
|
|
m[k] = v
|
|
|
|
}
|
2019-12-01 21:41:29 +08:00
|
|
|
for k, v := range r.queryMap {
|
2019-11-29 22:02:19 +08:00
|
|
|
m[k] = v
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-11-29 22:02:19 +08:00
|
|
|
return m
|
2018-07-29 22:01:29 +08:00
|
|
|
}
|
|
|
|
|
2019-11-29 22:02:19 +08:00
|
|
|
// GetQueryMapStrStr retrieves and returns all parameters passed from client using HTTP GET method
|
|
|
|
// as map[string]string. The parameter <kvMap> specifies the keys
|
|
|
|
// retrieving from client parameters, the associated values are the default values if the client
|
|
|
|
// does not pass.
|
2019-09-19 23:23:41 +08:00
|
|
|
func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
|
|
|
queryMap := r.GetQueryMap(kvMap...)
|
|
|
|
if len(queryMap) > 0 {
|
2019-10-30 23:26:57 +08:00
|
|
|
m := make(map[string]string, len(queryMap))
|
2019-09-19 23:23:41 +08:00
|
|
|
for k, v := range queryMap {
|
|
|
|
m[k] = gconv.String(v)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-29 22:02:19 +08:00
|
|
|
// GetQueryMapStrVar retrieves and returns all parameters passed from client using HTTP GET method
|
|
|
|
// as map[string]*gvar.Var. The parameter <kvMap> specifies the keys
|
|
|
|
// retrieving from client parameters, the associated values are the default values if the client
|
|
|
|
// does not pass.
|
2019-09-23 16:21:19 +08:00
|
|
|
func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
|
|
|
queryMap := r.GetQueryMap(kvMap...)
|
|
|
|
if len(queryMap) > 0 {
|
2019-10-30 23:26:57 +08:00
|
|
|
m := make(map[string]*gvar.Var, len(queryMap))
|
2019-09-23 16:21:19 +08:00
|
|
|
for k, v := range queryMap {
|
|
|
|
m[k] = gvar.New(v)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// GetQueryStruct retrieves all parameters passed from client using HTTP GET method
|
2019-11-29 22:02:19 +08:00
|
|
|
// and converts them to given struct object. Note that the parameter <pointer> is a pointer
|
|
|
|
// to the struct object. The optional parameter <mapping> is used to specify the key to
|
|
|
|
// attribute mapping.
|
2019-12-19 15:14:05 +08:00
|
|
|
func (r *Request) GetQueryStruct(pointer interface{}, mapping ...map[string]string) error {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseQuery()
|
2020-04-30 16:53:47 +08:00
|
|
|
return gconv.StructDeep(r.GetQueryMap(), pointer, mapping...)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-12-19 15:14:05 +08:00
|
|
|
|
|
|
|
// GetQueryToStruct is alias of GetQueryStruct. See GetQueryStruct.
|
|
|
|
// Deprecated.
|
|
|
|
func (r *Request) GetQueryToStruct(pointer interface{}, mapping ...map[string]string) error {
|
|
|
|
return r.GetQueryStruct(pointer, mapping...)
|
|
|
|
}
|