buffalo
buffalo copied to clipboard
Handling of multipart/form-data for JSON API
Hi. I'm handling a form with file upload (multipart/form-data) to an api endpoint (by this I mean I created the buffalo app with --api
) and then I also added middleware to set the content-type to application/json
.
My model looks something like this:
type Profile struct {
Username string `json:"username" db:"-"`
ProfileIcon binding.File `db:"-" form:"profileIcon"`
}
First attempt the file didn't upload (EOF error on bind). Then I skipped the json content-type middleware for that handler and I could see the file upload but then Username was empty.
After that I added form:"username"
struct tag to Username and then both values came through.
Is this the correct behavior?
Info
Buffalo Version
v0.11.0
App Information
Go Version
go version go1.10 linux/amd64
Go Env
GOARCH="amd64" GOBIN="" GOCACHE="??????????????" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/mnt/e/work/go" GORACE="" GOROOT="/usr/local/go" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1" 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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build186823332=/tmp/go-build -gno-record-gcc-switches"
Node Version
v6.10.0
NPM Version
3.10.10
Yarn Version
1.5.1
PostgreSQL Version
PostgreSQL Not Found
MySQL Version
mysql Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using EditLine wrapper
SQLite Version
3.11.0 2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f
Dep Version
dep: version : devel build date : git hash : go version : go1.10 go compiler : gc platform : linux/amd64 features : ImportDuringSolve=false
Dep Status
could not find project Gopkg.toml, use dep init to initiate a manifest
I would say yes. If I'm understanding correctly, you're submitting a (possibly custom?) form to a Buffalo generated API. It looks like adding the 'form:' tags provided the API with the info it needed to handle the submission and continue.
If you are dealing with small files, you could base64 encode them into a json and keep your json content-type middleware. If things start to get really ugly and hacky, maybe it's not the right question you are asking.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment. Otherwise, this will be closed in 7 days.
This issue was closed because it has been stalled for 30+7 days with no activity.