Copy link to clipboard
Copied
Can somebody explain how to define a remote function that will accept POST and then process a json object in the request body?
<cffunction name="examplePostEndpoint"
consumes="application/json"
produces="application/json"
httpMethod="POST"
returntype="string"
returnformat="json"
access="remote">
<!--- <cfset s = "#getHttpRequestData().content#"> --->
<!--- How to get body in here and convert to a struct? --->
<cfcontent type="application/json" >
<cfreturn serializeJSON({"some": "data"})>
</cffunction>
petrosm62752252 wrote
<script>
...
cosole.log("Executed");
...</script>
A spelling mistake you should get out of the way: "cosole".
orig%5Bcity%5D=Albuquerque&orig%5Bstate%5D=NM&orig%5Bcountry%5D=US&dests%5B0%5D%5Bcity%5D=Los+Angeles&dests%5B0%5D%5Bstate%5D=CA&dests%5B0%5D%5Bcountry%5D=US&dests%5B1%5D%5Bcity%5D=San+Diego&dests%5B1%5D%5Bstate%5D=CA&dests%5B1%5D%5Bcountry%5D=US&dests%5B2%5D%5Bcity%5D= New+York&dests%5B2%5D%5Bstate%5D=NY&dests%5B2%5D%5Bcountry%5D=US
Enclose this string in quotes and set
...Copy link to clipboard
Copied
In my opinion, you are there already!
Supposing that your current directory is called CFProject and that it is in the ColdFusion root. Then you have
\wwwroot\CFProject\hello.cfc:
<cfcomponent>
<cffunction name="examplePostEndpoint"
consumes="application/json"
produces="application/json"
httpMethod="POST"
returntype="string"
returnformat="json"
access="remote">
<cfargument name="inputString" type="string" >
<cfreturn serializeJSON(arguments.inputString)>
</cffunction>
</cfcomponent>
Some client code:
helloJohn1.cfm:
<cfset myJSONString = '{"name":"John", "age":"31", "city":"New York", "country":"USA"}'>
<cfform action="http://127.0.0.1:8500/CFProject/hello.cfc">
<cfinput name="inputString" type="text" value="#myJSONString#">
<cfinput name="method" type="text" value="examplePostEndpoint">
<cfinput name="sbmt" type="submit" value="send">
</cfform>
helloJohn2.cfm:
<cfset myJSONString = '{"name":"John", "age":"31", "city":"New York", "country":"USA"}'>
<cfform action="http://127.0.0.1:8500/CFProject/hello.cfc?method=examplePostEndpoint">
<cfinput name="inputString" type="text" value="#myJSONString#">
<cfinput name="sbmt" type="submit" value="send">
</cfform>
Copy link to clipboard
Copied
Thank you so much for your answer, but I am trying to post from jquery.
Anybody knows hot post from jquery using a json/text payload?
It's seems a super big flaw that this isn't supported in coldfusion
<script>
var data = {
"orig": {
"city": "Albuquerque", "state": "NM", "country": "US"
},
"dests": [
{"city": "Los Angeles", "state": "CA", "country": "US"},
{"city": "San Diego", "state": "CA", "country": "US"},
{"city": "New York", "state": "NY", "country": "US"}
]
};
$.ajax({
type: "POST",
url: "/tools/googlemaps/routing.cfc?method=getRoutesMatrixJson",
dataType: "json",
// data: JSON.stringify(data),
data: data,
success: function(data) {
console.log("Success:", data);
},
error: function(data) {
console.log("Failure:", data);
|
}
})cosole.log("Executed");
</script>
Copy link to clipboard
Copied
If I try to process this:
<cfset s = "#getHttpRequestData().content#">
<cflog text="#s#">
I get this
orig%5Bcity%5D=Albuquerque&orig%5Bstate%5D=NM&orig%5Bcountry%5D=US&dests%5B0%5D%5Bcity%5D=Los+Angeles&dests%5B0%5D%5Bstate%5D=CA&dests%5B0%5D%5Bcountry%5D=US&dests%5B1%5D%5Bcity%5D=San+Diego&dests%5B1%5D%5Bstate%5D=CA&dests%5B1%5D%5Bcountry%5D=US&dests%5B2%5D%5Bcity%5D=New+York&dests%5B2%5D%5Bstate%5D=NY&dests%5B2%5D%5Bcountry%5D=US
??????????
Copy link to clipboard
Copied
And the request content if passed through canonicalize
orig[city]=Albuquerque?ig[state]=NM?ig[country]=US&dests[0][city]=Los+Angeles&dests[0][state]=CA&dests[0][country]=US&dests[1][city]=San+Diego&dests[1][state]=CA&dests[1][country]=US&dests[2][city]=New+York&dests[2][state]=NY&dests[2][country]=US
So serialization happens I and I cannot prevent this.
Copy link to clipboard
Copied
petrosm62752252 wrote
<script>
...
cosole.log("Executed");
...</script>
A spelling mistake you should get out of the way: "cosole".
orig%5Bcity%5D=Albuquerque&orig%5Bstate%5D=NM&orig%5Bcountry%5D=US&dests%5B0%5D%5Bcity%5D=Los+Angeles&dests%5B0%5D%5Bstate%5D=CA&dests%5B0%5D%5Bcountry%5D=US&dests%5B1%5D%5Bcity%5D=San+Diego&dests%5B1%5D%5Bstate%5D=CA&dests%5B1%5D%5Bcountry%5D=US&dests%5B2%5D%5Bcity%5D= New+York&dests%5B2%5D%5Bstate%5D=NY&dests%5B2%5D%5Bcountry%5D=US
Enclose this string in quotes and set the value to the variable, JSONStr. You will see why the request failed when you do
<cfoutput>#URLDecode(JSONStr)#</cfoutput>
The result contains characters &. It suggests that this string consists of a series of key-value pairs in a URL query-string.
It's seems a super big flaw that this isn't supported in coldfusion
What you want is actually supported in ColdFusion. A possible solution is to add the content-type to your request. An example follows.
JSON_post_using_JQuery.cfm
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var JSONObject = {
orig: {
city: "Albuquerque", state: "NM", country: "US"
},
dests: [
{city: "Los Angeles", state: "CA", country: "US"},
{city: "San Diego", state: "CA", country: "US"},
{city: "New York", state: "NY", country: "US"}
]
};
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: "POST",
url: "http://127.0.0.1:8500/CFProject/JSONPost.cfc?method=examplePostEndpoint",
contentType: "application/json",
data: JSON.stringify(JSONObject),
success: function(data) {
console.log("Success:", data);
},
error: function(data) {
console.log("Failure:", data);
}
});console.log("Executed");
});
});
</script>
<button>Send HTTP POST request to CFC</button>
\wwwroot\CFProject\JSONPost.cfc
<cfcomponent >
<cffunction name="examplePostEndpoint"
consumes="application/json"
produces="application/json"
httpMethod="POST"
returntype="string"
returnformat="json"
access="remote">
<cflog text="#serializeJSON(getHttpRequestData().content)#" file="JSONPostTest">
</cffunction>
</cfcomponent>
Copy link to clipboard
Copied
Thank you so much.
I have tried many things even the example as stated,
for me to work I had to user deserializeJson and not serializeJson the very last code snippet you posted.
Thanks again,