Skip to content

How can I manually configure *resty.Request.RawRequest.ContentLength? #971

Open
@BoneAsh

Description

@BoneAsh

go 1.23.6
resty v3

I tested it using the net/http package, and when I send an HTTP request with the body as an io.Reader type, setting the Content-Length in the header does not work. I need to set req.ContentLength directly.

util.MyReader is a custom struct that implements the io.Reader interface, specifically the Read(p []byte) (n int, err error) method.

req, err := http.NewRequest("PUT", url, &util.MyReader{Data: []byte{0x00, 0x00}})
if err != nil {
	panic(err)
}

req.ContentLength = 2 // it works
#req.Header.Set("Content-Length", "2")  // it does not work

resp, err := client.Do(req)
if err != nil {
	panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
	panic(err)
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions