Upload File with Meta-Data (`multipart/form-data`) to ColdFusion 11 REST Service -
how access meta-data (form data) sent file upload (using enctype="multipart/form-data"
) in coldfusion 11 rest service?
mocking simple service:
component restpath = "test" rest = true { remove void function upload( required numeric id restargsource = "path", required document restargsource = "form", required string title restargsource = "form" ) httpmethod = "post" restpath = "{id}/" produces = "application/json" { if ( fileexists( document ) ) restsetresponse({ "status" = 201, "content" = { "id" = id, "title" = title, "size" = getfileinfo( document ).size } }); else restsetresponse({ "status" = 400, "content" = "nothing uploaded" }); } }
and simple html file test it:
<!doctype html> <html> <body> <form method="post" action="http://localhost/rest/testservice/test/1/" enctype="multipart/form-data"> <input type="text" name="title" value="file title" /><br /> <input type="file" name="document" /><br /> <button type="submit">submit</button> </form> </body> </html>
then service returns 500
http status code , json response:
{"message":"the document parameter upload function required not passed in."}
checking http request headers shows both fields passed in service not receiving them.
commenting out enctype
attribute within html form (so uses default encoding of application/x-www-form-urlencoded
) service returns 400
http status code , response nothing uploaded
.
how can resolved?
to thoroughly confusing request data populates form
variable but, though function arguments state restargsource="form"
, value not passed on rest service argument when enctype
other application/x-www-form-urlencoded
.
this confirmed in adobe's getting started restful web services in coldfusion article:
form parameters:
in many scenarios, must process data enter in form in rest service. can use data make new entry (
post
) or update existing record (put
) in database. if use form fields, setrestargsource
form
. extracts data present in request , makes available further processing. also, must set content-type headerapplication/x-www-form-urlencoded
when sending data form fields.
however, can solved changing arguments use restargsource="form"
parameters explicitly scoped within form
variable.
so, modifying service this:
component restpath = "test" rest = true { remove void function upload( required numeric id restargsource = "path" ) httpmethod = "post" restpath = "{id}/" produces = "application/json" { param name="form.document" type="string"; param name="form.title" type="string"; if ( fileexists( form.document ) ) restsetresponse({ "status" = 201, "content" = { "id" = id, "title" = form.title, "size" = getfileinfo( form.document ).size } }); else restsetresponse({ "status" = 400, "content" = "nothing uploaded" }); } }
Comments
Post a Comment