h2non / gentleman

Plugin-driven, extensible HTTP client toolkit for Go

Home Page:https://pkg.go.dev/github.com/h2non/gentleman?tab=doc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

the "net/http/httputil.DumpRequest" will destory the context value storage.

elonzh opened this issue · comments

package main

import (
	"fmt"
	"net/http/httputil"

	"gopkg.in/h2non/gentleman.v1"
	"gopkg.in/h2non/gentleman.v1/context"
)

func main() {
	req := gentleman.NewRequest()
	req.Method("GET")
	req.URL("httpbin.org/post")
	req.BodyString("Body Hell!")
	req.UseHandler("before dial", func(ctx *context.Context, h context.Handler) {
		fmt.Printf("Before setting context:%T - %v\n", ctx.Request.Body, ctx.Request.Body)
		ctx.Set("Foo", "Bar")
		fmt.Printf("After setting context:%T - %v\n", ctx.Request.Body, ctx.Request.Body)
		httputil.DumpRequest(ctx.Request, true)
		fmt.Printf("After DumpRequest:%T - %v\n", ctx.Request.Body, ctx.Request.Body)
		h.Next(ctx)
	})
	req.Do()
}

Output:

Before setting context:*context.contextReadCloser - &{{0xc042068d80} map[$phase:before dial]}
After setting context:*context.contextReadCloser - &{{0xc042068d80} map[Foo:Bar $phase:before dial]}
After DumpRequest:ioutil.nopCloser - {Body Hell!}

With so many issues caused by wrapping context storage in http.Request.Body , I think we urgently need a solution for context storing.

commented

With so many issues caused by wrapping context storage in http.Request.Body , I think we urgently need a solution for context storing.

Yeah, that was a design mistake, but I have plans to migrate to net/context in v2.
The good thing is that you're not restricted to use the built-in context provided in gentleman, you can now rely on net/context if you want.

The most important reason I using this project is the context. (The middleware and plugin system are also great features)

It will be close to perfect for me if this issue be solved.

Thanks for your work. It helped me save a lot of time, although I spent some time to debug. LOL

commented

I have just pushed v2 preview release, which relies on stdlib context and gets the rid of all the previous issues with the built-in context.

Code lives in v2 branch:
https://github.com/h2non/gentleman/tree/v2

There's also an outgoing PR that would be eventually merged once the new release is more mature and I had the change to port third-party plugins/packages too: #26

You should be able to use it via:

go get gopkg.in/h2non/gentleman.v2

Let me know how it works.

commented

Ping! Did you test this in gentleman@v2? If yes, please let me know how it works!

@h2non Yes! I am using gentleman@v2.

For now, it doesn't cause any issue in my use scene.

commented

gentleman@v2 is finally there: https://github.com/h2non/gentleman