Skip to main content
Participating Frequently
Oct 3, 2018 3:31 PM UTC
Answered

Example of a remote method with httpMethod GET and json payload

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>

    This topic is closed to new replies. Start a new post to keep the conversation going.
    BKBK
    BKBKCorrect answer
    Community Expert
    Oct 7, 2018 2:21 PM UTC

    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>

    6 replies

    BKBK
    Community Expert
    Community Expert
    Oct 4, 2018 6:55 PM UTC

    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>

    Translate
    Participating Frequently
    Oct 6, 2018 9:29 AM UTC

    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>

    Translate
    Participating Frequently
    Oct 6, 2018 9:32 AM UTC

    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

    ??????????

    Translate