request

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptsHTML

func AcceptsHTML(c *gin.Context) bool

AcceptsHTML 判断客户端是否接受 HTML 响应

func AcceptsJSON

func AcceptsJSON(c *gin.Context) bool

AcceptsJSON 判断客户端是否接受 JSON 响应

func Bind

func Bind(c *gin.Context, i any) error

Bind 绑定请求数据并自动校验 支持 JSON/Form/Query,具体绑定方式由 Gin 根据 Content-Type 决定

func BindJSON

func BindJSON(c *gin.Context, i any) error

BindJSON 绑定 JSON 请求体并自动校验

func BindQuery

func BindQuery(c *gin.Context, i any) error

BindQuery 绑定 Query 参数并自动校验

func BindUri

func BindUri(c *gin.Context, i any) error

BindUri 绑定路径参数并自动校验

func File

func File(c *gin.Context, key string) *multipart.FileHeader

File 获取单个上传文件,不存在时返回 nil

func Files

func Files(c *gin.Context, key string) []*multipart.FileHeader

Files 获取同名的多个上传文件,不存在时返回 nil

func GetBearerToken

func GetBearerToken(c *gin.Context) string

GetBearerToken 从 Authorization 头获取 Bearer Token

func GetClientIP

func GetClientIP(c *gin.Context) string

GetClientIP 获取客户端真实 IP 地址。 委托给 gin.Context.ClientIP(),由其依据可信代理(TrustedProxies)配置安全地解析 X-Forwarded-For/X-Real-IP;不再直接信任可被伪造的转发头。

func GetFullURL

func GetFullURL(c *gin.Context) string

GetFullURL 获取完整的请求 URL

func GetHost

func GetHost(c *gin.Context) string

GetHost 获取主机名

func GetMethod

func GetMethod(c *gin.Context) string

GetMethod 获取请求方法

func GetReferer

func GetReferer(c *gin.Context) string

GetReferer 获取 Referer

func GetScheme

func GetScheme(c *gin.Context) string

GetScheme 获取请求协议(http/https)

func GetUserAgent

func GetUserAgent(c *gin.Context) string

GetUserAgent 获取 User-Agent

func Input

func Input[T InputType](c *gin.Context, key string, defaultValue ...T) T

Input 按 key 读取请求参数并转换为目标类型 T,缺失或解析失败时返回默认值。

取值优先级:POST 表单 > Query > URL 路径参数(见 getRawValue / getArrayValues)。 注意:不读取 JSON 请求体;JSON 请求请使用 BindJSON 绑定到结构体。

支持的类型:

  • 基本类型: string, int, int64, float32, float64, bool
  • 数组类型: []string, []int, []int64

文件上传请使用 File / Files。

使用示例:

name := request.Input(c, "name", "默认名称")   // string
age := request.Input(c, "age", 18)            // int
price := request.Input(c, "price", 9.99)      // float64
active := request.Input(c, "active", true)    // bool
tags := request.Input(c, "tags", []string{})  // []string
ids := request.Input(c, "ids", []int{})       // []int

func IsAjax

func IsAjax(c *gin.Context) bool

IsAjax 判断是否为 AJAX 请求

func IsDELETE

func IsDELETE(c *gin.Context) bool

IsDELETE 判断是否为 DELETE 请求

func IsFormData

func IsFormData(c *gin.Context) bool

IsFormData 判断是否为表单数据请求

func IsGET

func IsGET(c *gin.Context) bool

IsGET 判断是否为 GET 请求

func IsJSON

func IsJSON(c *gin.Context) bool

IsJSON 判断是否为 JSON 请求

func IsLocalIP

func IsLocalIP(ip string) bool

IsLocalIP 判断是否为本地 IP

func IsMethod

func IsMethod(c *gin.Context, method string) bool

IsMethod 判断是否为指定的请求方法

func IsMobile

func IsMobile(c *gin.Context) bool

IsMobile 判断是否为移动设备访问

func IsMultipartForm

func IsMultipartForm(c *gin.Context) bool

IsMultipartForm 判断是否为 multipart/form-data 请求

func IsPATCH

func IsPATCH(c *gin.Context) bool

IsPATCH 判断是否为 PATCH 请求

func IsPOST

func IsPOST(c *gin.Context) bool

IsPOST 判断是否为 POST 请求

func IsPUT

func IsPUT(c *gin.Context) bool

IsPUT 判断是否为 PUT 请求

func IsSecure

func IsSecure(c *gin.Context) bool

IsSecure 判断是否为 HTTPS 请求

func IsXML

func IsXML(c *gin.Context) bool

IsXML 判断是否为 XML 请求

Types

type InputType

type InputType interface {
	string | int | int64 | float32 | float64 | bool |
		[]string | []int | []int64
}

InputType 定义 Input 函数支持的类型约束。 使用精确类型而非 ~ 近似约束:函数内部按精确类型分发, 若放开为派生类型会在运行时静默匹配失败,故在编译期就限定为这些精确类型。 文件上传请使用 File / Files 函数。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL