55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
|
// Package request ...
|
||
|
//
|
||
|
// Description : request ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2022-07-03 00:52
|
||
|
package request
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
// contentType 请求类型相关处理
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 00:53 2022/7/3
|
||
|
type contentType struct {
|
||
|
}
|
||
|
|
||
|
// IsJson 请求方式是否为JSON
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 00:35 2022/7/3
|
||
|
func (ct *contentType) IsJson(ctx *gin.Context) bool {
|
||
|
return strings.Contains(ct.Get(ctx), "json")
|
||
|
}
|
||
|
|
||
|
// IsFormURLEncoded 请求方式是否为 x-www-form-urlencoded
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 00:36 2022/7/3
|
||
|
func (ct *contentType) IsFormURLEncoded(ctx *gin.Context) bool {
|
||
|
return strings.Contains(ct.Get(ctx), "x-www-form-urlencoded")
|
||
|
}
|
||
|
|
||
|
// Get 获取请求类型
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 00:39 2022/7/3
|
||
|
func (ct *contentType) Get(ctx *gin.Context) string {
|
||
|
contentType := strings.ToLower(ctx.ContentType())
|
||
|
if len(contentType) == 0 {
|
||
|
// 请求不传type默认为 x-www-form-urlencoded
|
||
|
contentType = "application/x-www-form-urlencoded"
|
||
|
}
|
||
|
return contentType
|
||
|
}
|