Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: r.FormValue() ignores the return value of ParseMultipartForm() #20895

Closed
saleemjaffer opened this issue Jul 3, 2017 · 1 comment
Closed

Comments

@saleemjaffer
Copy link

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8.1 darwin/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/saleem/proj"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tx/9dlgzqg11c30qh8p6l3czn000000gn/T/go-build451820530=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

I have a Go Http endpoint that handles image uploads. From the client I make a POST request, with a content type multipart/form-data. I send some string key, value pairs and an image file.

On the server when I try to read the string values sent using request.FormValue("key_name") I get an empty string. This is the source code of request.FormValue from net/http:

func (r *Request) FormValue(key string) string {
	if r.Form == nil {
		r.ParseMultipartForm(defaultMaxMemory)
	}
	if vs := r.Form[key]; len(vs) > 0 {
		return vs[0]
	}
	return ""
}

The return value of ParseMultipartForm is ignored. It is the ParseMultipartForm function which is failing. Why is the return value ignored?

What did you expect to see?

The error returned from ParseMultipartForm should have been handled in request.FormValue of the net/http package

What did you see instead?

The error returned from ParseMultipartForm is not handled.

@saleemjaffer saleemjaffer changed the title net/http r.FormValue ignores the return value of ParseMultipartForm net/http r.FormValue() ignores the return value of ParseMultipartForm() Jul 3, 2017
@mvdan mvdan changed the title net/http r.FormValue() ignores the return value of ParseMultipartForm() net/http: r.FormValue() ignores the return value of ParseMultipartForm() Jul 3, 2017
@bradfitz
Copy link
Contributor

bradfitz commented Jul 3, 2017

Request.FormValue is a convenience, best-effort method. If it's not convenient, don't use it.

We can't change its signature (https://golang.org/doc/go1compat) even if we wanted to.

Typical caller code will check FormValue for acceptable values and return an HTTP 400 Bad Request or similar if the request is bogus or missing form values. If you really need to know specifically how the request is malformed, call ParseMultipartForm yourself.

@bradfitz bradfitz closed this as completed Jul 3, 2017
@golang golang locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants