以下是将enctype属性添加到表单标记并观察传递的参数丢失的简单示例。
## 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> </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."}
当包含enctype时,restArgSource是否应该不同?显然,http标头会改变,但是使用带有restargname的标头也不起作用......
答案 0 :(得分:0)
“另外,在将数据作为表单字段发送时,必须将content-type标头设置为application / x-www-form-urlencoded。”
所以你可能会因为运气不好而害怕。您可能能够获得底层http请求的句柄并访问请求体,但这是相当多的工作。使用多部分表单的原因是什么?我猜文件上传,但我有兴趣知道是否有另一个用例。
使用常规CFFile处理看起来像file upload itself will work in REST。如果你可以处理在URL上放置其他参数,你可能会得到一些可行的东西。