github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_request_param_post.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package ghttp 8 9 import ( 10 "github.com/gogf/gf/container/gvar" 11 "github.com/gogf/gf/util/gconv" 12 ) 13 14 // GetPost retrieves and returns parameter <key> from form and body. 15 // It returns <def> if <key> does not exist in neither form nor body. 16 // It returns nil if <def> is not passed. 17 // 18 // Note that if there're multiple parameters with the same name, the parameters are retrieved 19 // and overwrote in order of priority: form > body. 20 // 21 // Deprecated, use GetForm instead. 22 func (r *Request) GetPost(key string, def ...interface{}) interface{} { 23 r.parseForm() 24 if len(r.formMap) > 0 { 25 if v, ok := r.formMap[key]; ok { 26 return v 27 } 28 } 29 r.parseBody() 30 if len(r.bodyMap) > 0 { 31 if v, ok := r.bodyMap[key]; ok { 32 return v 33 } 34 } 35 if len(def) > 0 { 36 return def[0] 37 } 38 return nil 39 } 40 41 // Deprecated, use GetFormVar instead. 42 func (r *Request) GetPostVar(key string, def ...interface{}) *gvar.Var { 43 return gvar.New(r.GetPost(key, def...)) 44 } 45 46 // Deprecated, use GetFormString instead. 47 func (r *Request) GetPostString(key string, def ...interface{}) string { 48 return r.GetPostVar(key, def...).String() 49 } 50 51 // Deprecated, use GetFormBool instead. 52 func (r *Request) GetPostBool(key string, def ...interface{}) bool { 53 return r.GetPostVar(key, def...).Bool() 54 } 55 56 // Deprecated, use GetFormInt instead. 57 func (r *Request) GetPostInt(key string, def ...interface{}) int { 58 return r.GetPostVar(key, def...).Int() 59 } 60 61 // Deprecated, use GetFormInt32 instead. 62 func (r *Request) GetPostInt32(key string, def ...interface{}) int32 { 63 return r.GetPostVar(key, def...).Int32() 64 } 65 66 // Deprecated, use GetFormInt64 instead. 67 func (r *Request) GetPostInt64(key string, def ...interface{}) int64 { 68 return r.GetPostVar(key, def...).Int64() 69 } 70 71 // Deprecated, use GetFormInts instead. 72 func (r *Request) GetPostInts(key string, def ...interface{}) []int { 73 return r.GetPostVar(key, def...).Ints() 74 } 75 76 // Deprecated, use GetFormUint instead. 77 func (r *Request) GetPostUint(key string, def ...interface{}) uint { 78 return r.GetPostVar(key, def...).Uint() 79 } 80 81 // Deprecated, use GetFormUint32 instead. 82 func (r *Request) GetPostUint32(key string, def ...interface{}) uint32 { 83 return r.GetPostVar(key, def...).Uint32() 84 } 85 86 // Deprecated, use GetFormUint64 instead. 87 func (r *Request) GetPostUint64(key string, def ...interface{}) uint64 { 88 return r.GetPostVar(key, def...).Uint64() 89 } 90 91 // Deprecated, use GetFormFloat32 instead. 92 func (r *Request) GetPostFloat32(key string, def ...interface{}) float32 { 93 return r.GetPostVar(key, def...).Float32() 94 } 95 96 // Deprecated, use GetFormFloat64 instead. 97 func (r *Request) GetPostFloat64(key string, def ...interface{}) float64 { 98 return r.GetPostVar(key, def...).Float64() 99 } 100 101 // Deprecated, use GetFormFloats instead. 102 func (r *Request) GetPostFloats(key string, def ...interface{}) []float64 { 103 return r.GetPostVar(key, def...).Floats() 104 } 105 106 // Deprecated, use GetFormArray instead. 107 func (r *Request) GetPostArray(key string, def ...interface{}) []string { 108 return r.GetPostVar(key, def...).Strings() 109 } 110 111 // Deprecated, use GetFormStrings instead. 112 func (r *Request) GetPostStrings(key string, def ...interface{}) []string { 113 return r.GetPostVar(key, def...).Strings() 114 } 115 116 // Deprecated, use GetFormInterfaces instead. 117 func (r *Request) GetPostInterfaces(key string, def ...interface{}) []interface{} { 118 return r.GetPostVar(key, def...).Interfaces() 119 } 120 121 // GetPostMap retrieves and returns all parameters in the form and body passed from client 122 // as map. The parameter <kvMap> specifies the keys retrieving from client parameters, 123 // the associated values are the default values if the client does not pass. 124 // 125 // Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote 126 // in order of priority: form > body. 127 // 128 // Deprecated. 129 func (r *Request) GetPostMap(kvMap ...map[string]interface{}) map[string]interface{} { 130 r.parseForm() 131 r.parseBody() 132 var ok, filter bool 133 if len(kvMap) > 0 && kvMap[0] != nil { 134 filter = true 135 } 136 m := make(map[string]interface{}, len(r.formMap)+len(r.bodyMap)) 137 for k, v := range r.bodyMap { 138 if filter { 139 if _, ok = kvMap[0][k]; !ok { 140 continue 141 } 142 } 143 m[k] = v 144 } 145 for k, v := range r.formMap { 146 if filter { 147 if _, ok = kvMap[0][k]; !ok { 148 continue 149 } 150 } 151 m[k] = v 152 } 153 // Check none exist parameters and assign it with default value. 154 if filter { 155 for k, v := range kvMap[0] { 156 if _, ok = m[k]; !ok { 157 m[k] = v 158 } 159 } 160 } 161 return m 162 } 163 164 // GetPostMapStrStr retrieves and returns all parameters in the form and body passed from client 165 // as map[string]string. The parameter <kvMap> specifies the keys 166 // retrieving from client parameters, the associated values are the default values if the client 167 // does not pass. 168 // 169 // Deprecated. 170 func (r *Request) GetPostMapStrStr(kvMap ...map[string]interface{}) map[string]string { 171 postMap := r.GetPostMap(kvMap...) 172 if len(postMap) > 0 { 173 m := make(map[string]string, len(postMap)) 174 for k, v := range postMap { 175 m[k] = gconv.String(v) 176 } 177 return m 178 } 179 return nil 180 } 181 182 // GetPostMapStrVar retrieves and returns all parameters in the form and body passed from client 183 // as map[string]*gvar.Var. The parameter <kvMap> specifies the keys 184 // retrieving from client parameters, the associated values are the default values if the client 185 // does not pass. 186 // 187 // Deprecated. 188 func (r *Request) GetPostMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var { 189 postMap := r.GetPostMap(kvMap...) 190 if len(postMap) > 0 { 191 m := make(map[string]*gvar.Var, len(postMap)) 192 for k, v := range postMap { 193 m[k] = gvar.New(v) 194 } 195 return m 196 } 197 return nil 198 } 199 200 // GetPostStruct retrieves all parameters in the form and body passed from client 201 // and converts them to given struct object. Note that the parameter <pointer> is a pointer 202 // to the struct object. The optional parameter <mapping> is used to specify the key to 203 // attribute mapping. 204 // 205 // Deprecated. 206 func (r *Request) GetPostStruct(pointer interface{}, mapping ...map[string]string) error { 207 return gconv.Struct(r.GetPostMap(), pointer, mapping...) 208 } 209 210 // GetPostToStruct is alias of GetQueryStruct. See GetPostStruct. 211 // 212 // Deprecated. 213 func (r *Request) GetPostToStruct(pointer interface{}, mapping ...map[string]string) error { 214 return r.GetPostStruct(pointer, mapping...) 215 }