Question

Here is a simple example of adding enctype attribute to a form tag and watching the passed parameters become missing.

## test.rest.cfm
<!doctype html>
<html>
<head>
<title>Test Form</title>
</head>
<body>
<div>
<p>Will Post From Here</p>
<form method="post" action="http://devserver/rest/ost/testform/" name="frmName">
<table>
<tr>
<td>fname</td>
<td><input type="text" name="fname" id="fname" value="Fredley"></td>
</tr>
<tr>
<td>lname</td>
<td><input type="text" name="lname" value="Solongo"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Post"></td>
</tr>
</table>
</form>
</div>
</body>
</html>

## testFormHandler.cfc
<cfcomponent restpath="testform" rest="true" produces="application/JSON">

<cffunction description="POST method to test form"
name         = "postMethod"
access       = "remote"
output       = "false"
returntype   = "string"
returnformat = "json"
httpmethod   = "post" >

<cfargument required="1" type="any" restArgSource="Form" name="fname">
<cfargument required="1" type="any" restArgSource="Form" name="lname">

<cfset var arrayReturn = {}>
<cfset arrayReturn["fname"] = arguments.fname>
<cfset arrayReturn["lname"] = arguments.lname>

<cfreturn "{""PayLoad"":" & SerializeJSON(arrayReturn) & ",""Type"":""Success"",""Message"":""SUCCESS"",""Code"":""SUCCESS""}">

</cffunction>

</cfcomponent>

## change form tag of test.rest.cfm to gets param fname failure
<form method="post" action="http://dev1.ostit.com/rest/ost/testform/" name="frmName" enctype="multipart/form-data">

## results with no enctype
{"PayLoad":{"fname":"Fredley","lname":"Solongo"},"Type":"Success","Message":"SUCCESS","Code":"SUCCESS"}

## results with enctype added
{"Message":"The FNAME parameter to the postMethod function is required but was not passed in."}

Should the restArgSource be different when the enctype is included? Obviously, the http headers change, but using header with a restargname didn't work either...

Was it helpful?

Solution

The getting started guide says this:

"Also, you must set the content-type header to application/x-www-form-urlencoded when sending data as form fields."

So you may be out of luck I'm afraid. You may be able to get a handle to the underlying http request and access the request body like that, but it'd be a fair amount of work. What's the reasoning behind using multipart forms? I'd guess at file upload, but I'd be interested to know whether there's another use case.

It looks like file upload itself will work in REST using regular CFFile handling. If you can handle placing other parameters on the URL, you may get something workable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top