Golang - 在 echo api 中绑定标头
我正在尝试将标头绑定到结构。到目前为止,我所做的与此处的文档相同,但看起来根本不起作用。我检查了调试器传入请求,它具有正确的标头名称和值,但 echo 没有绑定它。
这是我的 API:
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
type User struct {
ID string `header:"Id"`
}
func handler(c echo.Context) error {
request := new(User)
if err := c.Bind(request); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.String(http.StatusOK, "rankView")
}
func main() {
api := echo.New()
api.POST("product/rank/view", handler)
api.Start(":3000")
}
和我的要求
curl -X POST "http://localhost:3000/product/rank/view" \
-H "accept: application/json" \
-H "Id: test" \
-H "Content-Type: application/x-www-form-urlencoded" -d "productId=123132"
繁星点点滴滴