在Gin中获取BasicAuth凭据

huangapple go评论102阅读模式
英文:

Get BasicAuth creds in Gin

问题

我已经切换到Gin来尝试一下。在这个转换之前,我通过请求对象访问BasicAuth凭据(app_id和token),代码如下:

  1. appId, token, _ := r.BasicAuth()

在每次调用中,需要在我的数据库中找到app_id,所以我在这里使用了Gin中间件:

  1. func CheckAppId() gin.HandlerFunc {
  2. return func(c *gin.Context) {
  3. // 在这里如何访问BasicAuth凭据?
  4. }
  5. }

但是我不确定如何在没有请求对象的情况下访问BasicAuth凭据。

英文:

I have made a switch to Gin to try it out. Before the move I accessed the BasicAuth credentials (app_id and token) using the request object like this:

  1. appId, token, _ := r.BasicAuth()

The app_id needs to be found in my database on every call so I'm using Gin middleware for this:

  1. func CheckAppId() gin.HandlerFunc {
  2. return func(c *gin.Context) {
  3. //how do I access the BasicAuth creds here?
  4. }
  5. }

but I'm not sure how to access the BasicAuth creds without the request object.

答案1

得分: 2

gin上下文中包含了http.Request对象,存储在Request字段中。

英文:

The gin context contains the http.Request object in the Request field.

huangapple
  • 本文由 发表于 2015年9月9日 01:16:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/32463626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定