Problem with cfhttp in a loop
Hi,
I am reading an xml file that contains emails with attachments (one email per item/node, but may contain multiple attachments). I am trying to save the attachments to our server. So I'm looping over the multiple attachments of a single email and using cfhttp to GET the attachments from another server and save them with the same filename on our server.
The problem is that it works for the first attachment - it's saved to the server, status code is 200 OK - awesome! But for any attachments after that, it does not save them and throws a Conection Failure error. No matter what attachments they are.
In trouble shooting I tried several things. First, inside of my loop of attachments, I can hardcode the cfhttp calls with the url and filename of the attachments - one right after the other - and all is perfect everytime!! But it obviously needs to be dynamic. I also tried to save a list of the attachment urls from the loop, and then call a separate cfhttp tag for each attachment in the list (so again, was in a loop) and it works for the first attachment in the list and not for the others (same errors as above).
Here's a simplified version of the code. I can't put in the real xml url, and when I set the "attachmentFilename" I left out that code because it works and is too much code. Also, the XML works fine. Please let me know if you have any suggestions, and of course, if you need more info from me!!
Thanks so much,
Kirsten
<cfoutput>
<cfhttp url="https://www.myxml.com/example.xml" method="get" resolveurl="no" />
<cfset myXML = trim(cfhttp.FileContent)>
<cfset myXML = xmlParse(myXML)>
<cfset theRoot = myXML.XmlRoot>
<cfset numChildren = arrayLen(theRoot.XMLChildren[1].XmlChildren)>
<cfloop index="i" from="6" to="#numChildren#">
<cfset attachments = theRoot.XMLChildren[1].XMLChildren["attachments"].XmlText>
<cfif ListLen(attachments, "|^|") gt 2>
<cfset loop_unid = theRoot.XMLChildren[1].XMLChildren["unid"].XmlText>
<cfset counter = 0>
<cfset attachmentArray = ListToArray(attachments, "|^|")>
<cfloop from="1" to="#ArrayLen(attachmentArray)#" index="k">
<cfset counter = counter + 1>
<cfset attachmentURL = attachmentArray
<cfset attachmentFilename = Replace(attachmentArray
<cfhttp url="#attachmentURL#" method="get" resolveurl="no" timeout="120" path="D:\my_servers_path\attachmentFolder\" file="#attachmentFilename#">
attachment counter: #counter#<BR />
cfhttp.statusCode: #cfhttp.statusCode#<BR />
cfhttp.errorDetail: #cfhttp.errorDetail#<BR />
</cfloop>
</cfif>
</cfloop>
</cfoutput>
Output Results:
attachment counter: 1
cfhttp.statusCode: 200 OK
cfhttp.errorDetail:
attachment counter: 2
cfhttp.statusCode: Connection Failure. Status code unavailable.
cfhttp.errorDetail: I/O Exception: peer not authenticated
attachment counter: 3
cfhttp.statusCode: Connection Failure. Status code unavailable.
cfhttp.errorDetail: I/O Exception: peer not authenticated
