3

So I'm trying to setup the API call to Twilio Flow using CFHTTP but am having no luck. Keeps returning CFHTTP doesn't exist when I try to view the response.

I've already tried adjusting from formfields to body, setting the charset to utf-8, etc. I was successfully able to send an SMS using the Programmable SMS portion but am having no luck hitting the Flow.

<cfset twilioUsername = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
<cfset twilioFlowSid = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
<cfset twilioPassword = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />

<cfset twilioFrom = "+1XXXXXXXXXX" />
<cfset twilioTo = "+1XXXXXXXXXX" />

<cfset bodyFields = {
    "name" : "Tester",
    "cases" : "This Case this Time"
} />

<cfset twilioFlowResource = (
    "https://studio.twilio.com/v1/Flows/#twilioFlowSid#/Executions.json"
    ) />

<cfhttp result="POST" method="POST" charset="utf-8" url="#twilioFlowResource#" username="#twilioUsername#" password="#twilioPassword#">
    <cfhttpparam type="formfield" name="From" value="twilioFrom" />
    <cfhttpparam type="formfield" name="To" value="twilioTo" />
    <cfhttpparam type="formfield" name="Parameters" value="#serializeJSON(bodyFields)#" />
</cfhttp>

All I keep receiving is variable CFHTTP doesn't exist when I try to view the contents of cfhttp.filecontent.

MTut
  • 35
  • 2
  • 1
    What version of ColdFusion – James A Mohler Jun 20 '19 at 16:12
  • 2
    It is because you used the `result` attribute which *".. Lets you specify an alternate variable in which to receive a result"*. Meaning CF won't populate the default variable name `cfhttp`. In [your example](https://trycf.com/gist/d0e7e6e2aebd989ec180eb93409d1130/acf2016?theme=monokai), you should dump the variable name "POST". Though for clarity, I'd recommend using a different variable name. – SOS Jun 20 '19 at 16:22

1 Answers1

2

It is because you used cfhttp's "result" attribute which ".. lets you specify an alternate variable in which to receive a result". Meaning CF won't populate the default variable named cfhttp. So in your example, you should be dumping the variable named #POST#. (Though to avoid further confusion, I'd recommend using something else, like "response".)

<cfhttp result="response" 
    method="POST" 
    charset="utf-8" 
    url="#twilioFlowResource#" 
    username="#twilioUsername#" 
    password="#twilioPassword#">

    ... parameters ...
</cfhttp>

<cfdump var="#response#">

Also, perhaps it's just a typo, but ... if that's the actual code you're using, it's missing pound signs around the variables in the <cfhttpparam> declarations. So the code is actually sending the literal string "twilioFrom" instead of the variable value: +1XXXXXXXXXX. These lines:

<cfhttpparam type="formfield" name="From" value="twilioFrom" />
<cfhttpparam type="formfield" name="To" value="twilioTo" />

... should be changed to this:

<cfhttpparam type="formfield" name="From" value="#twilioFrom#" />
<cfhttpparam type="formfield" name="To" value="#twilioTo#" />
SOS
  • 6,430
  • 2
  • 11
  • 29
  • Thanks so much, yes the value missing pound signs were just typos adjusting things for posting on here, but the result was the issue. I had this in there as it worked for posting to their SMS gateway but broke this part for the Flow gateway – MTut Jun 20 '19 at 16:52
  • You're welcome :-) BTW, the reason I suggest changing the variable name is that even I missed the "result" variable on first read. I saw "POST" and my brain kicked into autocompletion gear and translated it into *method* = "POST" ;-) – SOS Jun 20 '19 at 17:02