增加获取query和body的方法
This commit is contained in:
@ -8,8 +8,11 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
@ -79,11 +82,19 @@ func (wh *wrapperHandle) GetScheme(ctx *gin.Context, defaultVal string) string {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:03 2024/1/2
|
||||
func (wh *wrapperHandle) GetQuery(ctx *gin.Context) url.Values {
|
||||
func (wh *wrapperHandle) GetQuery(ctx *gin.Context) map[string]string {
|
||||
query := make(map[string]string)
|
||||
if nil != ctx && nil != ctx.Request && nil != ctx.Request.URL {
|
||||
return ctx.Request.URL.Query()
|
||||
for paramName, valueList := range {
|
||||
if len(valueList) == 0 {
|
||||
query[paramName] = ""
|
||||
} else {
|
||||
query[paramName] = valueList[0]
|
||||
}
|
||||
}
|
||||
return query
|
||||
}
|
||||
return make(url.Values)
|
||||
return query
|
||||
}
|
||||
|
||||
// GetMethod 获取请求方法
|
||||
@ -111,6 +122,38 @@ func (wh *wrapperHandle) GetContentType(ctx *gin.Context, defaultVal string) str
|
||||
return wrapper.TernaryOperator.String(len(contentType) > 0, wrapper.String(contentType), wrapper.String(defaultVal)).Value()
|
||||
}
|
||||
|
||||
// GetRequestBody 获取请求body
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:25 2024/7/26
|
||||
func (wh *wrapperHandle) GetRequestBody(ctx *gin.Context) map[string]any {
|
||||
body := map[string]any{}
|
||||
contentType := wh.GetContentType(ctx, "")
|
||||
if strings.Contains(contentType, consts.MimeTypeJson) { // json请求
|
||||
data, err := io.ReadAll(ctx.Request.Body)
|
||||
if nil != err {
|
||||
return body
|
||||
}
|
||||
ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data))
|
||||
return body
|
||||
}
|
||||
if strings.Contains(contentType, consts.MimeTypeXWWWFormUrlencoded) { // form请求
|
||||
if err := ctx.Request.ParseForm(); nil != err {
|
||||
return body
|
||||
}
|
||||
for paramName, itemParam := range ctx.Request.PostForm {
|
||||
if len(itemParam) > 0 {
|
||||
body[paramName] = itemParam[0]
|
||||
} else {
|
||||
body[paramName] = ""
|
||||
}
|
||||
}
|
||||
return body
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
// GetResponseBody 获取响应body
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
Reference in New Issue
Block a user