POST rest with FORM defines all variables

Im getting variables set regardless if they are passed into the request call as long any form data is submitted.

 <cffunction httpmethod = "POST" name="test" access="remote" output="false" restpath="test">
        <cfargument name="ateat"  type="string"  >

       <cfreturn ateat>
    </cffunction>

POstman:

Same result with this API endpoint

  <cffunction httpmethod = "POST" name="test" access="remote" output="false" restpath="test">
        <cfargument name="ateat" default ="1"  >

       <cfreturn ateat>
    </cffunction>

Hi @kpetrow ,

I can’t say what’s wrong with your code or environmental issues causing the problem. However, I was just working on something similar. Here’s how I (would) set up code to perform the REST functionality.

Component /my_api/scribble.cfc

component restpath="/scribble" rest="true" {

/** ===================================================
 * /rest/my_api/scribble/test  ========= HTTP POST ====
 * ====================================================
 */
    remote any function test(
        required string ateat restargsource = "form"
    ) 
        httpmethod = "post" restpath = "test"
    {
        var payload = {'success':false, 'ateat'=''};
        var componentAndFunctionName = "#GetComponentMetaData(this).name#.#GetFunctionCalledName()#(): ";
        try {
            payload['ateat'] = arguments.ateat;
            payload['success'] = true;
            restSetResponse({
                status: 200
                , content: serializeJSON(payload)
                , headers: {'Content-Type': 'application/json'}
            });
            writeDump(var = '== #componentAndFunctionName# End of try block.', output = 'console');
        }
        catch(any e) {
            writeDump(var = '== #componentAndFunctionName# #e.message# #e.detail#', output = 'console');
            restSetResponse({ 
                status: 500,      
                content: serializeJSON(e),                   
                headers: {'Content-Type': 'application/json'} 
            });
        }
        finally {
            writeDump(var = '== #componentAndFunctionName# REST component was called.', output = 'console');
        }
    }
       
}

Calling the REST service in CFML:

    rslt = {};
    http 
	    url = "http://localhost/rest/my_api/scribble/test" 
	    method = "post" 
	    result = "rslt" 
	{
        httpparam 
	        type = "formfield" 
	        name = "ateat" 
	        value = "111";
    }   
    writeDump(rslt);

Calling the REST service from JavaScript:

	fetch(`http://localhost/rest/my_api/scribble/test`, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		body: `ateat=1`
	})
	.then(response => response.json())
	.then(data => {
		console.log('Success:', data);
	})
	.catch((error) => {
		console.error('Error:', error);
	});

Please note that I did change some file and path names to protect the innocent but this basic setup works for me in Lucee 5 & 6.

I hope this helps!

Ray

Let’s first get some possible misunderstandings out of the way. That should perhaps be return arguments.ateat instead of return ateat. Just a matter of correctness because, in any case, Lucee will likely ignore your argument in favour of the one posted to the API. Also, the function’s return-type should probably be specified. However, these are not the cause of the issue.

Like @x999 , I suspect that what you have shown is caused by an environmental variable that was posted to the API. I arrived at that conclusion as follows.

I used your data and did the following REST test on Lucee 5.4.2.17. Everything worked as expected.

// The file /ROOT/restTest/Application.cfc
component  {
	// set a name for the REST application:
	this.name = "myRestApplication";
	this.webAdminPassword="my_admin_password";

	public boolean function onApplicationStart()
	{
		// Define a REST service with same name as the application
		restInitApplication( 
			dirPath=getDirectoryFromPath(getCurrentTemplatePath()), 
			serviceMapping=this.name, 
			password="my_admin_password"
		);
	            
		return true;
	}
    
	public boolean function onRequestStart()
	{         
		return true;
	}
}
<!--- File /ROOT/restTest/TestComp.cfc --->
<cfcomponent rest="true" restpath="/test">
	<!--- @hint: path of REST call = /rest/myRestApplication/test/testIt,
	  where myRestApplication = name of REST api as set in Application.cfc.
        --->
	<cffunction httpmethod = "POST" name="testFunc" access="remote" output="false" restpath="testIt">
          <cfargument name="ateat"  type="string"  >
		<cfreturn ateat>
	</cffunction>
</cfcomponent>
<!--- CFM page to call REST API: /ROOT/restTest/callToTestRestApi.cfm --->
<cfhttp 
    url="http://localhost:8888/rest/myRestApplication/test/testIt" 
    method="POST" 
    result="result"> 
    <cfhttpparam name="ateat" value="6" type="formfield">	
</cfhttp>
<cfdump var = "#result#" label="REST Test Result">

The result of the call is:

Perhaps the environment is configured by default to convert responses to a format you don’t expect. Suggestions from the tests:

  1. experiment by omitting the line <cfargument name="ateat" type="string" >.
  2. give the function a returntype / returnformat.

why is it returning the field name? There is definitely some screwy behavior happening. How does cfargument work with form fields?

I used cfargument in my test. The value posted in the form is 6. What the API returns is not just the field name, but the string “ateat=6”. I think the explanation for that is: the default return-format is JSON.

I have just repeated the test, omitting the cfargument line this time. The value posted in the form is again 6. The return value this time is “6”, in contrast to “ateat=6” in the previous test.