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

ctx.Request().body only fetch once #1297

Closed
wyanlord opened this issue Jul 12, 2019 · 4 comments
Closed

ctx.Request().body only fetch once #1297

wyanlord opened this issue Jul 12, 2019 · 4 comments
Labels
good first issue A user wrote a good first issue with clear instructions 🤘 status:resolved 🐞 type:bug
Milestone

Comments

@wyanlord
Copy link

I get it in middleware. And I cannot get it in handler again.

@kataras
Copy link
Owner

kataras commented Jul 12, 2019

Hello @wyanlord, this is a net/http (comes from Body.Read method which consumes data this is basic golang knowedge) behavior.

data, err := ioutil.ReadAll(ctx.Request().Body)
if err != nil {
	return err
}

ctx.Request().Body = ioutil.NopCloser(bytes.NewBuffer(data))

Hope that helped.

@wyanlord
Copy link
Author

I hope I can keep it in the iris.context structure after form param parsed.
This native body is used in many places.

@wyanlord
Copy link
Author

wyanlord commented Jul 12, 2019

// 获取iris的请求体
func RequestBody(ctx context.Context) string {
	var requestBody string
	data, err := ioutil.ReadAll(ctx.Request().Body)
	if err == nil {
		requestBody = string(data)
		ctx.Request().Body = ioutil.NopCloser(bytes.NewBuffer(data))
	}
	return requestBody
}

// 请求日志记录
func NewAccessLogMdw() iris.Handler {
	return func(ctx context.Context) {
		fmt.Println(helper.RequestBody(ctx))

		fmt.Println(ctx.FormValues())

		fmt.Println(helper.RequestBody(ctx))

		ctx.Next()
	}
}

when post http://localhost:8080/users?c=5 with b=3.
The result is:

b=3
map[b:[3] c:[5]]
nil

And I had used iris.WithoutBodyConsumptionOnUnmarshal

kataras added a commit that referenced this issue Jul 12, 2019
…ctx.FormValues' and on the new 'ctx.GetBody' method helper as requested at: #1297
@kataras
Copy link
Owner

kataras commented Jul 12, 2019

So your issue here was the ctx.FormValues.

Fixed with bf3f306 on the upcoming v11.2 release.

That was a good bug report, thank you a lot @wyanlord and sorry for the duplicate label but please next time provide me a code snippet like this from the first place so I can be more helpful

@kataras kataras added this to the v11.2.0 milestone Jul 12, 2019
@kataras kataras added the good first issue A user wrote a good first issue with clear instructions label Jul 12, 2019
@kataras kataras closed this as completed Jul 23, 2019
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
…ctx.FormValues' and on the new 'ctx.GetBody' method helper as requested at: kataras#1297

Former-commit-id: c6a52681c940681ac85a330465d38a078186a8a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue A user wrote a good first issue with clear instructions 🤘 status:resolved 🐞 type:bug
Projects
None yet
Development

No branches or pull requests

2 participants