gf/g/net/ghttp/http_client_request.go

209 lines
5.3 KiB
Go
Raw Normal View History

2017-12-29 16:03:30 +08:00
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// 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,
// You can obtain one at https://gitee.com/johng/gf.
2017-11-23 10:21:28 +08:00
package ghttp
import (
"io/ioutil"
"gitee.com/johng/gf/g/encoding/gjson"
2017-12-31 11:09:16 +08:00
"gitee.com/johng/gf/g/util/gconv"
2017-11-23 10:21:28 +08:00
)
// 获取当前请求的id
func (r *ClientRequest) Id() uint64 {
return r.id
}
2017-12-08 09:50:11 +08:00
// 获得指定名称的get参数列表
2017-11-23 10:21:28 +08:00
func (r *ClientRequest) GetQuery(k string) []string {
if r.getvals == nil {
values := r.URL.Query()
r.getvals = &values
}
if v, ok := (*r.getvals)[k]; ok {
return v
}
return nil
}
2017-12-31 11:09:16 +08:00
func (r *ClientRequest) GetQueryBool(k string) bool {
return gconv.Bool(r.GetQueryString(k))
}
2017-12-08 09:50:11 +08:00
func (r *ClientRequest) GetQueryInt(k string) int {
2017-12-31 11:09:16 +08:00
return gconv.Int(r.GetQueryString(k))
}
func (r *ClientRequest) GetQueryUint(k string) uint {
return gconv.Uint(r.GetQueryString(k))
}
func (r *ClientRequest) GetQueryFloat32(k string) float32 {
return gconv.Float32(r.GetQueryString(k))
}
func (r *ClientRequest) GetQueryFloat64(k string) float64 {
return gconv.Float64(r.GetQueryString(k))
2017-12-08 09:50:11 +08:00
}
2017-11-23 10:21:28 +08:00
func (r *ClientRequest) GetQueryString(k string) string {
v := r.GetQuery(k)
if v == nil {
return ""
} else {
return v[0]
}
}
func (r *ClientRequest) GetQueryArray(k string) []string {
2017-12-31 11:09:16 +08:00
return r.GetQuery(k)
2017-11-23 10:21:28 +08:00
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
2017-12-28 17:28:32 +08:00
func (r *ClientRequest) GetQueryMap(defaultMap map[string]string) map[string]string {
m := make(map[string]string)
2017-11-23 10:21:28 +08:00
for k, v := range defaultMap {
v2 := r.GetQueryArray(k)
if v2 == nil {
m[k] = v
} else {
2017-12-28 17:28:32 +08:00
m[k] = v2[0]
2017-11-23 10:21:28 +08:00
}
}
return m
}
// 获得post参数
func (r *ClientRequest) GetPost(k string) []string {
if v, ok := r.PostForm[k]; ok {
return v
}
return nil
}
2017-12-31 11:09:16 +08:00
func (r *ClientRequest) GetPostBool(k string) bool {
return gconv.Bool(r.GetPostString(k))
}
2017-12-08 09:50:11 +08:00
func (r *ClientRequest) GetPostInt(k string) int {
2017-12-31 11:09:16 +08:00
return gconv.Int(r.GetPostString(k))
}
func (r *ClientRequest) GetPostUint(k string) uint {
return gconv.Uint(r.GetPostString(k))
}
func (r *ClientRequest) GetPostFloat32(k string) float32 {
return gconv.Float32(r.GetPostString(k))
}
func (r *ClientRequest) GetPostFloat64(k string) float64 {
return gconv.Float64(r.GetPostString(k))
2017-12-08 09:50:11 +08:00
}
2017-11-23 10:21:28 +08:00
func (r *ClientRequest) GetPostString(k string) string {
v := r.GetPost(k)
if v == nil {
return ""
} else {
return v[0]
}
}
func (r *ClientRequest) GetPostArray(k string) []string {
2017-12-31 11:09:16 +08:00
return r.GetPost(k)
2017-11-23 10:21:28 +08:00
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
2017-12-28 17:28:32 +08:00
// 需要注意的是如果其中一个字段为数组形式那么只会返回第一个元素如果需要获取全部的元素请使用GetPostArray获取特定字段内容
func (r *ClientRequest) GetPostMap(defaultMap map[string]string) map[string]string {
m := make(map[string]string)
2017-11-23 10:21:28 +08:00
for k, v := range defaultMap {
if v2, ok := r.PostForm[k]; ok {
2017-12-28 17:28:32 +08:00
m[k] = v2[0]
2017-11-23 10:21:28 +08:00
} else {
m[k] = v
}
}
return m
}
// 获得post或者get提交的参数如果有同名参数那么按照get->post优先级进行覆盖
func (r *ClientRequest) GetRequest(k string) []string {
v := r.GetQuery(k)
if v == nil {
return r.GetPost(k)
}
return v
}
func (r *ClientRequest) GetRequestString(k string) string {
v := r.GetRequest(k)
if v == nil {
return ""
} else {
return v[0]
}
}
2017-12-31 11:09:16 +08:00
func (r *ClientRequest) GetRequestBool(k string) bool {
return gconv.Bool(r.GetRequestString(k))
}
func (r *ClientRequest) GetRequestInt(k string) int {
return gconv.Int(r.GetRequestString(k))
}
func (r *ClientRequest) GetRequestUint(k string) uint {
return gconv.Uint(r.GetRequestString(k))
}
func (r *ClientRequest) GetRequestFloat32(k string) float32 {
return gconv.Float32(r.GetRequestString(k))
}
func (r *ClientRequest) GetRequestFloat64(k string) float64 {
return gconv.Float64(r.GetRequestString(k))
}
2017-11-23 10:21:28 +08:00
func (r *ClientRequest) GetRequestArray(k string) []string {
2017-12-31 11:09:16 +08:00
return r.GetRequest(k)
2017-11-23 10:21:28 +08:00
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
2017-12-28 17:28:32 +08:00
// 需要注意的是如果其中一个字段为数组形式那么只会返回第一个元素如果需要获取全部的元素请使用GetRequestArray获取特定字段内容
func (r *ClientRequest) GetRequestMap(defaultMap map[string]string) map[string]string {
m := make(map[string]string)
2017-11-23 10:21:28 +08:00
for k, v := range defaultMap {
v2 := r.GetRequest(k)
if v2 != nil {
2017-12-28 17:28:32 +08:00
m[k] = v2[0]
2017-11-23 10:21:28 +08:00
} else {
m[k] = v
}
}
return m
}
// 获取原始请求输入字符串
func (r *ClientRequest) GetRaw() []byte {
result, _ := ioutil.ReadAll(r.Body)
return result
2017-11-23 10:21:28 +08:00
}
2017-12-31 11:09:16 +08:00
// 获取原始json请求输入字符串并解析为json对象
2017-11-23 10:21:28 +08:00
func (r *ClientRequest) GetJson() *gjson.Json {
data := r.GetRaw()
if data != nil {
if j, err := gjson.DecodeToJson(data); err == nil {
return j
}
2017-11-23 10:21:28 +08:00
}
return nil
}