Skip to main content
Participant
December 14, 2010
Question

Variable CFHTTP is undefined

  • December 14, 2010
  • 1 reply
  • 1697 views

Hello folks,

I have a script that uses the CFHTTP tag to send in some XML to a url, which returns a code back to my script.

01.    <cftry>

02.        <cfhttp

03.             url="http://the.url.to.post.to"
04.             port="443"
05.             method="post"
06.             resolveurl="yes"
07.             throwOnError="Yes"
08.             timeout="5"
09.             charset="utf-8">
10.             <cfhttpparam type="header" name="Accept-Encoding" value="*">

11.             <cfhttpparam type="Header" name="TE" value="deflate;q=0">

12.             <cfhttpparam type="formField" name="xmlBody" value="#toString(generateRequestXml)#">

13.      </cfhttp>

14.

15.      <!--- if http request was complete, and "ok" --->

16.      <cfif structkeyexists(cfhttp,'statuscode') and listfirst("#cfhttp.statuscode#",' ') eq 200>

17.           <!--- hooray, the http request worked fine --->

18.           <cfset theVariable = cfhttp.returnedVariable>

19.

20.      <!--- else, something went wrong --->

21.      <cfelse>

22.           <!--- oh no - something went wrong with the request --->

23.           <cfthrow />

24.      </cfif>

25.

26.      <cfcatch type="any">

27.           <cfset logError(cfcatch)>

28.      </cfcatch>

29. </cftry>

Most of the time, the script works fine, except now and then, it throws an error on line 16 saying that "Variable CFHTTP is undefined". It's as if the cfhttp tag has not finished processing in order to create the "CFHTTP" variable. This sounds a bit like the problem that Charlie Arehart blogged a while back about CF not waiting on some elements - or am I just presuming here?

Anyway - some help would be really useful here, as I'm really scratching my head on this one. I've even tried replicating the error with a CFHTTP url which takes a while to return a response - but no joy.

Help me Adobe Community - you're my only hope....

(heh, I just made that up!! - you can use it)

Cheers,

Phil

    This topic has been closed for replies.

    1 reply

    Community Expert
    December 14, 2010

    I don't know if the problem is caused by the CFHTTP tag not being finished yet, although that seems as possible as anything else I guess. If that's the problem, though, you could address this by creating a loop with a condition, then within the loop you could test for the existence of CFHTTP, and if it doesn't exist you could put the thread to sleep for a few milliseconds and check again after that. Once it does exist, you could exit the loop.

    Dave Watts, CTO, Fig Leaf Software

    http://www.figleaf.com/

    http://training.figleaf.com/

    Dave Watts, Eidolon LLC
    philmunroAuthor
    Participant
    December 14, 2010

    Wow Dave,

    Thanks for that. I had a loop which would check for the variable, then try

    again - but this was only trying to create ANOTHER cfhttp thread. If there

    was something that was slowing down cfhttp creation (e.g. heavy load on the

    CF instance) then this would likely just create multiple cfhttp threads that

    would each have the same problem.

    I'll try what you said and post it up here!

    Cheers,

    Phil