Skip to content

When the API parameter setting in tag is header, the field configuration custom header key is invalid. #3245

@shuqingzai

Description

@shuqingzai

使用下面的示例代码,分别打开 HeaderName 的 3 个 case 测试

package main

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

type HelloReq struct {
	g.Meta `path:"/hello" method:"get"`
	Name   string `json:"name" v:"required" dc:"Your name"`
	// 1. 指定 json 标签 header key -- 无效
	// 字段名: HeaderName Header传递的key: X-Header-Name
	// HeaderName string `json:"X-Header-Name" in:"header" v:"required" dc:"Your Header name"`
	// 2. 字段名: HeaderName 指定 p 标签 header key -- 无效
	// 字段名: HeaderName Header传递的key: X-Header-Name
	// HeaderName string `p:"X-Header-Name" in:"header" json:"X-Header-Name" v:"required" dc:"Your Header name"`
	// 3. 只有 字段名 与 header key 相同(PascalCase ==> kebab-case)才有效 -- 有效
	// 字段名: XHeaderName Header传递的key: X-Header-Name
	XHeaderName string `in:"header" json:"X-Header-Name" v:"required" dc:"Your Header name"`
}
type HelloRes struct {
	Reply any
}

type Hello struct{}

func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) {
	g.Log().Debugf(ctx, `receive say: %+v`, req)
	res = &HelloRes{
		Reply: req,
	}
	return
}

func main() {
	s := g.Server()
	s.SetPort(8800)
	s.Use(ghttp.MiddlewareHandlerResponse)
	s.Group("/", func(group *ghttp.RouterGroup) {
		group.Bind(
			new(Hello),
		)
	})
	s.Run()
}

测试代码

curl --location --request GET 'http://127.0.0.1:8800/hello?name=test' \
--header 'X-Header-Name: test header' \
--header 'User-Agent: GF-API-TESTTING' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8800' \
--header 'Connection: keep-alive'

根据文档所说可以使用 p 标签设置字段名称,但是似乎在 header key 中无效,仅在请求参数有效
参考:请求输入请求校验#示例1,基本使用

What version of Go and system type/arch are you using?

#-> % go version
go version go1.21.5 darwin/amd64

What version of GoFrame are you using?

-> % gf --version
v2.6.1
Welcome to GoFrame!
Env Detail:
  Go Version: go1.21.5 darwin/amd64
  GF Version(go.mod):
    github.com/gogf/gf/[email protected]
CLI Detail:
  Installed At: /Users/Code/go/bin/gf
  Built Go Version: go1.21.5
  Built GF Version: v2.6.1
Others Detail:
  Docs: https://goframe.org
  Now : 2024-01-03T16:01:15+08:00

Can this bug be re-produced with the latest release?

YES

What did you do?

What did you expect to see?

What did you see instead?

Metadata

Metadata

Assignees

Labels

bugIt is confirmed a bug, but don't worry, we'll handle it.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions