Skip to content

net/http: client Expect: 100-continue reads first byte of input on failure #16002

Closed
@ncw

Description

@ncw

I'm trying to use Expect: 100-continue from the net/http client so that I can retry (after refreshing the token) requests with a body when I get a 401 request.

Do do this I'm passing in an io.Reader into http.NewRequest and setting the header thus

req, err := http.NewRequest("PUT", url, body)
// err handling
req.Header.Set("Expect", "100-continue")

However what I find is that 1 byte gets read from the reader even if the request fails with a 401 error.

This means that I can't retry the request as 1 byte is now missing from the reader.

It might be that this is working as intended, in which case this behavior should be documented.

Playground demonstration

What version of Go are you using (go version)?

Fails on stable and tip

go version devel +3c6b668 Thu Jun 2 00:22:03 2016 +0000 linux/amd64
go version go1.6.2 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/ncw/Code/Go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

What did you do?

See https://play.golang.org/p/3jXlWyrgsa for example program

What did you expect to see?

No bytes read from reader on 401 error

What did you see instead?

1 bytes read from the reader

2009/11/10 23:00:01 Starting fetch with req = &{Method:PUT URL:http://127.0.0.1:18398/ Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[Expect:[100-continue]] Body:{Reader:0x10779700} ContentLength:0 TransferEncoding:[] Close:false Host:127.0.0.1:18398 Form:map[] PostForm:map[] MultipartForm:<nil> Trailer:map[] RemoteAddr: RequestURI: TLS:<nil> Cancel:<nil>}
2009/11/10 23:00:01 reader.Read(1)
2009/11/10 23:00:01  -> 1 bytes
2009/11/10 23:00:01 Got PUT request with headers map[User-Agent:[Go-http-client/1.1] Expect:[100-continue] Accept-Encoding:[gzip]]
2009/11/10 23:00:01 Got expected 401 error
2009/11/10 23:00:01 reader.Read(512)
2009/11/10 23:00:01  -> 22 bytes
2009/11/10 23:00:01 reader.Read(1514)
2009/11/10 23:00:01  -> io.EOF
2009/11/10 23:00:01 Remains of body "ody should not be read"
2009/11/10 23:00:01 shouldn't have read body, but read 1 bytes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions