Golang Gin 获取 HTTP 请求头中的 User Agent 值

文章目录

    Golang Gin Web 框架中有两种获取 HTTP 请求头中的 User Agent 值的方法:

    方法一

    func Index(c *gin.Context) {
        ua := c.GetHeader("User-Agent")
        // do something ...
    }
    

    方法二

    ua2 := c.Request.Header.Get("User-Agent")
    

    结果示例

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36