Skip to content

net/http: Content-Length is not set in outgoing request when using ioutil.NopCloser #34295

Closed
@johansja

Description

@johansja

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

$ go version
go version go1.13 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/1041775/Library/Caches/go-build"
GOENV="/Users/1041775/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/1041775/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.13/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.13/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/1041775/projects/js-scripts/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d6/809nhvwd23nd_wryrw60j6hdms016p/T/go-build703352534=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Start a httpbin server locally.

docker run -p 80:80 kennethreitz/httpbin

Run the following program

package main

import (
	"bytes"
	"io/ioutil"
	"log"
	"net/http"
)

func main() {
	reqBody := ioutil.NopCloser(bytes.NewBufferString(`{}`))
	req, err := http.NewRequest("POST", "http://localhost:80/post", reqBody)
	if err != nil {
		log.Fatalf("Cannot create request: %v", err)
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatalf("Cannot do: %v", err)
	}
	defer res.Body.Close()
	resBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatalf("Cannot read body: %v", err)
	}
	log.Printf("Response Body: %s", resBody)
}

What did you expect to see?

Content-Length header is set when it is received by the server.

What did you see instead?

Content-Length header is missing when it is received by the server.

2019/09/14 12:55:00 Response Body: {
  "args": {}, 
  "data": "{}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "localhost:80", 
    "Transfer-Encoding": "chunked", 
    "User-Agent": "Go-http-client/1.1"
  }, 
  "json": {}, 
  "origin": "172.17.0.1", 
  "url": "http://localhost:80/post"
}

Versus what I would receive if I use

reqBody := bytes.NewBufferString({})
.

2019/09/14 12:55:22 Response Body: {
  "args": {}, 
  "data": "{}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "2", 
    "Host": "localhost:80", 
    "User-Agent": "Go-http-client/1.1"
  }, 
  "json": {}, 
  "origin": "172.17.0.1", 
  "url": "http://localhost:80/post"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions