为什么无法读取标头值?

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

Why is the header value not being read?

问题

package middleware

import (
	"fmt"

	"github.com/labstack/echo/v4"
)

type _getData struct {
	Token string `header:"Authorization"`
}

func TokenCheck(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		a := new(_getData)
		c.Bind(a)
		fmt.Print(a)
		return next(c)
	}
}

代码中有一个在请求头中的授权值,使用 echo 框架的 bind 方法,我想知道授权值,但是没有返回值。

英文:
package middleware

import (
"fmt"

"github.com/labstack/echo/v4"
)

type _getData struct {
Token string `header:"Authorization"`
}

func TokenCheck(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
	a := new(_getData)
	c.Bind(a)
	fmt.Print(a)
	return next(c)
}
}

There is an authrozation value in the header, using bind to the echo framework, I want to know the authrozation value, but there is no returned value.

答案1

得分: 3

BindHeadersBind中没有被调用。源代码如下:

func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
	if err := b.BindPathParams(c, i); err != nil {
		return err
	}
	// Issue #1670 - Query params are binded only for GET/DELETE and NOT for usual request with body (POST/PUT/PATCH)
	// Reasoning here is that parameters in query and bind destination struct could have UNEXPECTED matches and results due that.
	// i.e. is `&id=1&lang=en` from URL same as `{"id":100,"lang":"de"}` request body and which one should have priority when binding.
	// This HTTP method check restores pre v4.1.11 behavior and avoids different problems when query is mixed with body
	if c.Request().Method == http.MethodGet || c.Request().Method == http.MethodDelete {
		if err = b.BindQueryParams(c, i); err != nil {
			return err
		}
	}
	return b.BindBody(c, i)
}

你可以通过以下方式获取头部的值:

func TokenCheck(next echo.HandlerFunc) echo.HandlerFunc {
	b := &echo.DefaultBinder{}
	return func(c echo.Context) error {
		a := new(_getData)
		b.BindHeaders(c, a)
		fmt.Print(a)
		return next(c)
	}
}

或者:

token := c.Request().Header.Get("Authorization")
英文:

BindHeaders was not called in Bind. The Source code:

func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
	if err := b.BindPathParams(c, i); err != nil {
		return err
	}
	// Issue #1670 - Query params are binded only for GET/DELETE and NOT for usual request with body (POST/PUT/PATCH)
	// Reasoning here is that parameters in query and bind destination struct could have UNEXPECTED matches and results due that.
	// i.e. is `&id=1&lang=en` from URL same as `{"id":100,"lang":"de"}` request body and which one should have priority when binding.
	// This HTTP method check restores pre v4.1.11 behavior and avoids different problems when query is mixed with body
	if c.Request().Method == http.MethodGet || c.Request().Method == http.MethodDelete {
		if err = b.BindQueryParams(c, i); err != nil {
			return err
		}
	}
	return b.BindBody(c, i)
}

You can get the value in the header in the following ways:

func TokenCheck(next echo.HandlerFunc) echo.HandlerFunc {
	b := &echo.DefaultBinder{}
	return func(c echo.Context) error {
		a := new(_getData)
		b.BindHeaders(c, a)
		fmt.Print(a)
		return next(c)
	}
}

Or:

token := c.Request().Header.Get("Authorization")

huangapple
  • 本文由 发表于 2021年9月24日 09:38:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/69308569.html
匿名

发表评论

匿名网友

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

确定