Skip to content

net/http: Content-Length: 0 is not set for PATCH requests with empty body #40978

Closed
@tharinduwijewardane

Description

@tharinduwijewardane

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

1.15

Does this issue reproduce with the latest release?

yes

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

darwin amd64

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN="/Users/tharindu/GoCode/bin"
GOCACHE="/Users/tharindu/Library/Caches/go-build"
GOENV="/Users/tharindu/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/tharindu/GoCode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/tharindu/GoCode"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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/3l/cw1f6lvn6p1c_vcg0cnbmqwc0000gp/T/go-build547496291=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func main() {

	url := "https://myapi.net/logs/file/a?action=aa"
	method := "PATCH"

	payload := strings.NewReader("")
	client := &http.Client {
	}
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		fmt.Println(err)
	}
	req.Header.Set("Authorization", "Bearer my-token")
	req.Header.Set("Content-Length", "0")

	res, err := client.Do(req)
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)

	fmt.Println(string(body))
}

What did you expect to see?

Content-Length header getting set to 0 when the request is observed from the server side.

What did you see instead?

Content-Length header is not getting set for PATCH requests with empty/nil payload. It is getting set without any issue for other http methods (POST, PUT).
It is also getting set for PATCH requests if the payload is non empty.
Also setting req.Header.Set("Content-Length", "0") seems to have no effect on the actual content length getting set in the request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions