buffalo icon indicating copy to clipboard operation
buffalo copied to clipboard

Handling of multipart/form-data for JSON API

Open pieterlouw opened this issue 6 years ago • 2 comments

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

VCS=git WithPop=true WithDep=false WithWebpack=false WithYarn=false WithDocker=true WithGrifts=true

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

pieterlouw avatar Apr 13 '18 13:04 pieterlouw

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.

forrest321 avatar May 05 '18 13:05 forrest321

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.

petar-dambovaliev avatar May 07 '18 10:05 petar-dambovaliev

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.

github-actions[bot] avatar Oct 27 '22 02:10 github-actions[bot]

This issue was closed because it has been stalled for 30+7 days with no activity.

github-actions[bot] avatar Nov 04 '22 02:11 github-actions[bot]