Skip to content

net/http: Error adds newline to message #50186

Closed
@YetAnotherRussian

Description

@YetAnotherRussian

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

go1.17.5 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

Microsoft Windows [Version 6.3.9600]

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\DMusin\AppData\Local\go-build
set GOENV=C:\Users\DMusin\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Go\bin\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Go\bin
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.5
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Temp\go-b
uild1689227927=/tmp/go-build -gno-record-gcc-switches

What did you do?

I use a small program to test GET requests to API:

package main

import (
"fmt"
"log"
"net"
"net/http"
"strings"
//"encoding/json"
)

func process(w http.ResponseWriter, r *http.Request) {

switch r.Method {
case "GET":
	key := r.Header.Get("API-key")
		if key != "123-345-678" {
		fmt.Println("The request was missing a valid API key!")
		http.Error(w, "", http.StatusForbidden)
		return
		}
    http.ServeFile(w, r, "list.html")
case "POST":
    fmt.Fprintln(w, "Method not implemented yet.")
	return
default:
    fmt.Fprintln(w, "Method not supported!")
}

}

func GetLocalIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()

return strings.Split(conn.LocalAddr().String(), ":")[0]

}

func main() {
fmt.Println("Server is starting...")
http.HandleFunc("/", process)
localIp := GetLocalIP() + ":12345"
fmt.Println("Bound to " + localIp)
log.Fatal(http.ListenAndServe(localIp, nil))
}


What did you expect to see?

http.Error(w, "", http.StatusForbidden) - expected an empty message here
http.Error(w, "Wrong API key!", http.StatusForbidden) - expected "Wrong API key!" message

What did you see instead?

http.Error(w, "", http.StatusForbidden) - "\n" message here
http.Error(w, "Wrong API key!", http.StatusForbidden) - "Wrong API key!\n" message here

From Chrome:
image

From Visual Studio:
image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions