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

net/http/httputil: DumpRequestOut error when POST responds with 301 #47161

Closed
VeNoMouS opened this issue Jul 13, 2021 · 3 comments
Closed

net/http/httputil: DumpRequestOut error when POST responds with 301 #47161

VeNoMouS opened this issue Jul 13, 2021 · 3 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@VeNoMouS
Copy link

VeNoMouS commented Jul 13, 2021

There appears to be a bug with httputil.DumpRequestOut ... with body set to true and doing a POST request and if the server responds with a 301 ... it errors out...

The error that you get is http: ContentLength=2265 with Body length 0 from transfer.go in writeBuf

	if !t.ResponseToHEAD && t.ContentLength != -1 && t.ContentLength != ncopy {
		return fmt.Errorf("http: ContentLength=%d with Body length %d",
			t.ContentLength, ncopy)
	}

If you remove the DumpRequestOut , everything works fine...

@seankhliao
Copy link
Member

I can't reproduce this with

func main() {
	r := bufio.NewReader(strings.NewReader("xxx")) // skip http.NewRequest setting Request.GetBody
	req, err := http.NewRequest("POST", "http://localhost:8080/redirect", r)
	if err != nil {
		log.Fatal(err)
	}

	dump, err := httputil.DumpRequestOut(req, true)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(dump))
	_, err = http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
}

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 13, 2021
@mdlayher mdlayher changed the title Bug with httputil.DumpRequestOut when POST responds with 301 net/http/httputil: DumpRequestOut error when POST responds with 301 Jul 14, 2021
@VeNoMouS
Copy link
Author

VeNoMouS commented Jul 14, 2021

I was doing this after a .Do(req) the problem with dumprequestout , you have to do it after the .do as cookies etc arent added in prior to the .do so it isnt a true "capture" of outgoing data if your doing it on the wire prior to the request...

@seankhliao
Copy link
Member

Request bodies are io.Readers, they can only be read once and in your case, when the request is made.

Closing as this is working was intended

@golang golang locked and limited conversation to collaborators Jul 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants