Skip to main content
Known Participant
July 3, 2012
Answered

Passing CF variables in soap request

  • July 3, 2012
  • 2 replies
  • 13457 views

Hello,

I have been successful at getting a return from a third party API. Now I need to use that return (session key) on other soap requests in their API in order to retrieve information. I am storing the session key as a variable and was hoping to use this in subsequent calls to their API, however, it is not working. I am getting an error that tells me the session key is 'invalid and is outside the bounds of an array'. Does anyone know if it is possible to store and pass variables in susbsequest soap requests? Any help would be greatly appreciated!!!!

<!--- WSDL --->
<cfset wsdl_url="http://someurl?wsdl">

<!--- Compose SOAP message to send to Web Service--->
    <cfsavecontent variable="soap"><?xml version="1.0" encoding="UTF-8" ?>
         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
             <soapenv:Body>
                 <Login xmlns="http://www.siretechnologies.com/">
      <LicenseKey>licenseKey</LicenseKey>
      <Username>user</Username>
      <Password>pass</Password>
      <LicenseType>2</LicenseType>
      <APIKey>APIKey</APIKey>
      <SiteKey></SiteKey>
      <CryptKey></CryptKey>
      <WebOnly>false</WebOnly>
    </Login>
              </soapenv:Body>
          </soapenv:Envelope>
    </cfsavecontent>


        <!--- Invoke web service to send message--->
        <cfhttp url="#wsdl_url#" method="post" >
            <cfhttpparam type="header" name="content-type" value="text/xml">
            <cfhttpparam type="header" name="SOAPAction" value="http://www.siretechnologies.com/Login">
            <cfhttpparam type="header" name="content-length" value="#len(soap)#">
            <cfhttpparam type="header" name="charset" value="utf-8">
            <cfhttpparam type="xml" name="message" value="#trim(soap)#">
        </cfhttp>
        <p><cfoutput>#xmlFormat(cfhttp.fileContent)#</cfoutput> </p>
        <cfset MyXml = XmlParse(cfhttp.fileContent)>

  <cfdump var="#MyXml#">
 
<cfset responseNodes = xmlSearch(MyXml,"//*[ local-name() = 'LoginResponse' ]")>
     <cfdump var="#responseNodes#">

<cfoutput>
<cfloop from="1" to="#arraylen(responseNodes)#" index="i">
    <cfset BookXML = xmlparse(responseNodes)>
<cfset SesKey = "#BookXML.LoginResponse.LoginResult.XmlText#">
    <b>SessionKey:</b> #BookXML.LoginResponse.LoginResult.XmlText#<br>
    <b>Session Key:#SesKey#
</cfloop>
</cfoutput>

<!--- GETUserId, Next call to API--->
<cfset wsdl_url="http://someurl/sire.asmx?wsdl">

<cfset sesKey2 = "#SesKey#">

  

<!--- Compose SOAP message to send to Web Service--->
    <cfsavecontent variable="soap"><?xml version="1.0" encoding="UTF-8" ?>
         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
             <soapenv:Body>
                 <GetUserId xmlns="http://www.siretechnologies.com/">
                 <SessionKey>sesKey2</SessionKey>
      <UserName>user</UserName>
                 </GetUserId>
               </soapenv:Body>
          </soapenv:Envelope>
    </cfsavecontent>

    This topic has been closed for replies.
    Correct answer itisdesign_-_AH

    You are the best Aaron! I will mark everyone who helped.

    When I run the script you provided with the credentials needed. I get a page that reads:

    "1st API call did not return XML"

    Also I am not getting any other errors.

    Thanks!!!


    Emily LaMunyon wrote:

    When I run the script you provided with the credentials needed. I get a page that reads:

    "1st API call did not return XML"

    Also I am not getting any other errors.

    Hi Emily,

    You're welcome, and that message indicates that the 1st call doesn't even return properly.  So we can disregard the 2nd call for the time being.  Could you try this, and let us know what the output is?

    <!--- WSDL --->

    <cfset wsdl_url="http://someurl/sirewebsvc/sire.asmx?wsdl">

    <!--- Compose SOAP message to send to Web Service--->

    <cfxml variable="soap">

    <?xml version="1.0" encoding="UTF-8" ?>

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

      <soapenv:Body>

        <Login xmlns="http://www.siretechnologies.com/">

          <LicenseKey>licenseKey</LicenseKey>

          <Username>survweb</Username>

          <Password>survweb</Password>

          <LicenseType>2</LicenseType>

          <APIKey>API</APIKey>

          <SiteKey></SiteKey>

          <CryptKey></CryptKey>

          <WebOnly>false</WebOnly>

        </Login>

      </soapenv:Body>

    </soapenv:Envelope>

    </cfxml>

    <!--- Invoke web service to send message--->

    <cfhttp url="#wsdl_url#" method="post" >

    <cfhttpparam type="header" name="content-type" value="text/xml">

    <cfhttpparam type="header" name="SOAPAction" value="http://www.siretechnologies.com/Login">

    <cfhttpparam type="header" name="content-length" value="#len(trim(soap))#">

    <cfhttpparam type="header" name="charset" value="utf-8">

    <cfhttpparam type="xml" name="message" value="#trim(soap)#">

    </cfhttp>

    <cfdump var="#CFHTTP.FileContent#" />

    Thanks!,

    -Aaron

    2 replies

    BKBK
    Community Expert
    Community Expert
    July 5, 2012

    <cfloop from="1" to="#arraylen(responseNodes)#" index="i">

        <cfset BookXML = xmlparse(responseNodes)>

    <cfset SesKey = "#BookXML.LoginResponse.LoginResult.XmlText#">

        <b>SessionKey:</b> #BookXML.LoginResponse.LoginResult.XmlText#<br>

        <b>Session Key:#SesKey#

    </cfloop>

    Yet another point besides those Reed Powell has discussed. You are looping across the array responseNodes, but overwriting the one value of sesKey each time. Shouldn't you define sesKey as an array, for example, and write the values like this instead?

    <cfset sesKey = arrayNew(1)>

    <cfloop from="1" to="#arraylen(responseNodes)#" index="i">

        <cfset BookXML = xmlparse(responseNodes)>

        <cfset SesKey = BookXML.LoginResponse.LoginResult.XmlText>

        <b>SessionKey:</b> #SesKey#<br>

    </cfloop>

    Known Participant
    July 5, 2012

    Thanks BKBK!!

    I tried your suggestion and am still not able to pass the seesion key variable correctly in the bolded text below. Do you think the session key is being overwritten, and that is why it won't work twice? Any other ideas????

    <cfloop from="1" to="#arraylen(responseNodes)#" index="i">

        <cfset BookXML = xmlparse(responseNodes)>

        <cfset SesKey = "#BookXML.LoginResponse.LoginResult.XmlText#">

        <b>SessionKey:</b> #BookXML.LoginResponse.LoginResult.XmlText#<br>

        <b>Session Key:#SesKey#

    </cfloop>

    </cfoutput>

    <!--- GETFILEPDF --->

    <cfset wsdl_url2="http://slcsireapp-tst/sirewebsvc/sire.asmx?wsdl">

        <!--- Compose SOAP message to send to Web Service--->

        <cfsavecontent variable="soap"><?xml version="1.0" encoding="UTF-8" ?>

             <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

                 <soapenv:Body>

                     <GetUserId xmlns="http://www.siretechnologies.com/">

                     <SessionKey>#SesKey#</SessionKey>

                      <UserName>survweb</UserName>

                     </GetUserId>

                   </soapenv:Body>

              </soapenv:Envelope>

        </cfsavecontent>

    itisdesign
    Inspiring
    July 5, 2012

    Emily LaMunyon wrote:

    <cfset SesKey = "#BookXML.LoginResponse.LoginResult.XmlText#">

    Hi Emily,

    If you <cfdump var="#SesKey#" /> are the values identical or unique in every row?

    Thanks,

    -Aaron

    Inspiring
    July 3, 2012

    You forgot the # marks around the sesKey2 variable in the SOAP request.

    Inspiring
    July 3, 2012

    didn't mean to het send so soon.  I think you are mis-thinking the action of the line of code

    <cfset sesKey2 = "#SesKey#">

    Putting the hash marks inside of the var isn't going to make it automatically evaluate a reference to sesKey2.  All you probalby need to do is to remove that line of code, and in the SOAP replace

    <SessionKey>sesKey2</SessionKey>

    with

    <SessionKey>#sesKey#</SessionKey>

    Is the username really "user"? or is that another case where the username is in a var called "user", in which case you'll need hash marks there as well.

    -reed


    Known Participant
    July 3, 2012

    Thanks for the response Reed!

    I tried your suggestion and am still getting the same error, it is like the variable is not being passed from above.

    <SessionKey>#sesKey#</SessionKey>

    Also, user is just a filler for the forum, it is really survweb and that seems to be working well.

    Any other thoughts?