Skip to content

net/http: Double slash in request leads Method to always be GET #69063

Closed as not planned
@heathcliff26

Description

@heathcliff26

Go version

go version go1.23.0 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOENV='/home/user/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/user/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/user/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/user/repos/testrepo/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3255200026=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I accidentally created a double slash in an http request URL. I have written the following to reproduce:

Server server.go:

package main

import "net/http"

func main() {
	http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
		if req.Method != http.MethodPost {
			rw.WriteHeader(http.StatusBadRequest)
			rw.Write([]byte("Wrong request type: " + req.Method))
		} else {
			rw.Write([]byte("Success"))
		}
	})

	err := http.ListenAndServe("localhost:8080", nil)
	if err != nil {
		panic(err)
	}
}

Client client.go:

package main

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

func main() {
	req, err := http.NewRequest(http.MethodPost, "http://localhost:8080/"+"/foo/bar", strings.NewReader("test"))
	if err != nil {
		panic(fmt.Errorf("Failed to create request: %v", err))
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(fmt.Errorf("Failed to send request: %v", err))
	}
	buf, err := io.ReadAll(res.Body)
	if err != nil {
		panic(fmt.Errorf("Failed to read response body: %v", err))
	}
	fmt.Println(string(buf))
}

What did you see happen?

Server receives a GET request, with no errors on the client/server side

What did you expect to see?

Server receives a POST request, or possible an error when creating the request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions