Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法获取requestBodyStream #949

Closed
mozhu811 opened this issue Sep 18, 2023 · 9 comments
Closed

无法获取requestBodyStream #949

mozhu811 opened this issue Sep 18, 2023 · 9 comments
Labels
question Further information is requested

Comments

@mozhu811
Copy link

Describe the Question

在开启StreamBody的情况下,获取到的bodyStream为noBody

Reproducible Code

func main() {
	h := server.Default(server.WithStreamBody(true))
	register(h)
	h.Spin()
}

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots
前端调用代码
image

端点调试
image

Hertz version:

v0.6.7

Environment:

The output of go env.

Additional context

Add any other context about the question here.

@chaoranz758
Copy link
Member

可以参考下这里试试 https://www.cloudwego.io/zh/docs/hertz/tutorials/basic-feature/engine/#%E6%B5%81%E5%BC%8F%E8%AF%BB

@li-jin-gou
Copy link
Member

是不是发的 GET 请求。

@li-jin-gou
Copy link
Member

是不是发的 GET 请求。

和 GET 请求没关系,本地最新没有复现

@chaoranz758
Copy link
Member

是不是发的 GET 请求。

他前端代码显示的是 post 请求

@welkeyever
Copy link
Member

不太应该,看看能否提供一个postman/curl的复现方式 @Cruii

@li-jin-gou li-jin-gou added the question Further information is requested label Sep 18, 2023
@mozhu811
Copy link
Author

curl调用

curl --location 'http://localhost:8888/webauthn/reg/verify' \
--header 'Content-Type: application/json' \
--data '{
    "attr": {
        "id": "rlCtfJsTRSxhl9cH65xvEi8UOIM_O1UyY__Ij1I_-qE",
        "rawId": "rlCtfJsTRSxhl9cH65xvEi8UOIM_O1UyY__Ij1I_-qE",
        "response": {
            "attestationObject": "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YVikSZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2NFAAAAAAAAAAAAAAAAAAAAAAAAAAAAIK5QrXybE0UsYZfXB-ucbxIvFDiDPztVMmP_yI9SP_qhpQECAyYgASFYIFiboyWQGfqZh_4zRT3H8BVXxb4Ek3MmGEvxAj3fM1TVIlgg7bpcX5y9t9Q2vI54OjYJDuSVRovqVPx05MmofsL1VAM",
            "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoielVqR3hWTGJGQ3RudXZnMW84RGlXNGF2TmR3Y3Q2alRwUkdOanZHOVhjNCIsIm9yaWdpbiI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTE3MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
            "transports": [
                "internal"
            ],
            "publicKeyAlgorithm": -7,
            "publicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWJujJZAZ-pmH_jNFPcfwFVfFvgSTcyYYS_ECPd8zVNXtulxfnL231Da8jng6NgkO5JVGi-pU_HTkyah-wvVUAw",
            "authenticatorData": "SZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2NFAAAAAAAAAAAAAAAAAAAAAAAAAAAAIK5QrXybE0UsYZfXB-ucbxIvFDiDPztVMmP_yI9SP_qhpQECAyYgASFYIFiboyWQGfqZh_4zRT3H8BVXxb4Ek3MmGEvxAj3fM1TVIlgg7bpcX5y9t9Q2vI54OjYJDuSVRovqVPx05MmofsL1VAM"
        },
        "type": "public-key",
        "clientExtensionResults": {},
        "authenticatorAttachment": "platform"
    }
}'

handler

// VerifyRegistration .
// @router /webauthn/reg/verify [POST]
func VerifyRegistration(ctx context.Context, c *app.RequestContext) {
	var err error
	var req webauthnModel.WebauthnRegVerifyReq
	err = c.BindAndValidate(&req)
	if err != nil {
		c.String(consts.StatusBadRequest, err.Error())
		return
	}
	s := c.RequestBodyStream()
	fmt.Println(s) // 输出 {},断点调试类型 protocol.noBody
}

main

func main() {
	redis.InitClient()
	svcclient.InitSvcClient()
	h := server.Default(server.WithStreamBody(true), server.WithTransport(standard.NewTransporter))
	h.Use(cors.New(cors.Config{
		AllowAllOrigins: true,
	}))
	err := webauthn.Init()
	if err != nil {
		panic(err)
	}

	register(h)
	h.Spin()
}

@chaoranz758
Copy link
Member

如果用流式读请求内容的话就不要先用 BindAndValidate 绑定了,两者选一个即可

@mozhu811
Copy link
Author

了解了,也就是说BindAndValidate处理后会把流关闭吗

@li-jin-gou
Copy link
Member

了解了,也就是说BindAndValidate处理后会把流关闭吗

BindAndValidate 调用 .Body 绑定 binding json 参数
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

4 participants