8 Replies Latest reply on Dec 15, 2007 12:24 AM by BKBK

    returning function value

    vncntj
      this is my .cfc
      <cfcomponent>
      <cffunction name="httpTransaction" access="public"
      returntype="string">
      <cfargument name="USER" type="string" required="yes">
      <cfargument name="VENDOR" type="string" required="yes">

      <CFHTTP url="" method="post" resolveurl="no" timeout="30"
      <CFHTTPPARAM type="header" name="X-VPS-REQUEST-ID"
      value="#CreateUUID()#">
      <CFHTTPPARAM type="header" name="X-VPS-CLIENT-TIMEOUT" value="30">
      <CFHTTPPARAM name="USER" type="formfield" value="#USER#" encoded="no">
      <CFHTTPPARAM name="VENDOR" type="formfield" value="#VENDOR#"
      encoded="no">
      </CFHTTP>


      <!--- how do i return the value --->


      </cffunction>
      </cfcomponent>


      and I'm trying to pass the


      <cfoutput>CFHTTP.FileContent#</cfoutput>to the page that is invoking
      the above function.
      Thanks,
        • 1. Re: returning function value
          Level 7
          RTFM

          <cffunction ...>
          <cfargument1 ...>
          <cfargument2 ...>
          ....
          <cfset var myResult = "">
          ...
          <cfreturn myResult>
          </cffunction>

          hth

          ---
          Azadi Saryev
          Sabai-dee.com
          http://www.sabai-dee.com
          • 2. Re: returning function value
            BKBK Adobe Community Professional & MVP
            I think it's only appropriate to return the cfhttp entity that Coldfusion creates after running the tag. I think its a structure, but can't recall now. So I use type "any". The result is:

            <cffunction name="httpTransaction" access="public"
            returntype="any" output="false">

            <!--- ... etc., etc., ... --->

            <cfreturn cfhttp>
            </cffunction>

            On the calling page you would then have:
            <cfset httpObj = createobject ("component", "component_path")>
            <cfset myResult = httpObj.httpTransaction(myUser, myVendor)>
            <cfset content = myResult.fileContent>

            P.S.
            - give url attribute a value;
            - scope your vars, hence arguments.USER and arguments.VENDOR

            • 3. Re: returning function value
              vncntj Level 1
              I'm a little confused on:
              <cfset myResult = httpObj.httpTransaction(myUser, myVendor)>

              are the variables "myUser", "myVendor", coming from the scope of the values that we've already passed to the cfarguments? I'm wondering if we are passing those same variables again?

              thanks,

              • 4. Re: returning function value
                vncntj Level 1
                Wait. I think I spoke too soon. I'll try something. It's usually after you move the piece that you see it's a bad move.
                • 5. Re: returning function value
                  vncntj Level 1
                  OK, it all made sense, but I have one last question. I'm getting this error message,

                  "You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members".

                  He is what we put together:

                  <cfset requestData = StructNew()>
                  <CFSET requestData.VENDOR = "john">
                  <CFSET requestData.USER = "doe">

                  <CFSET httpObj = createObject("component", "Caller")>
                  <CFSET myResult = httpObj.httpTransaction(requestData.VENDOR,requestData.USER )>
                  <CFSET content = #myResult.fileConent#>

                  <CFOUTPUT>#content#</CFOUTPUT>
                  • 6. Re: returning function value
                    freakyAndFree Level 1
                    Look -- your httpTransaction() function is returning a string. That string is referenced in your code as 'myResult'. Therefore, just change this:
                    <CFSET content = #myResult.fileConent#>
                    <CFOUTPUT>#content#</CFOUTPUT>

                    to this:

                    <cfoutput>#myResult#</cfoutput>
                    • 7. Re: returning function value
                      vncntj Level 1
                      You rock. Thanks for the inspiration!!
                      • 8. returning function value
                        BKBK Adobe Community Professional & MVP
                        Error message:
                        "You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members".

                        I advised you to return the struct cfhttp, but you continued to return a string instead. Hence the error.

                        I think it's much better for your code to return the cfhttp structure rather than a string. Just change the return type to "struct" or to "any". Then let the function end with: <cfreturn cfhttp>.

                        There are at least two good reasons to return a structure. First, you thereby make much more information available to the caller. Secondly, it enables the calling page to do some validation, for example, like this: