Skip to main content
Known Participant
January 25, 2011
Question

xml urlencoded and body data problem

  • January 25, 2011
  • 2 replies
  • 2134 views

Hi there,

I have an xml body which needs to be sent to an API, I have done  many of this and I'm connecting correctly to the API but for  some  reason I can't get this one to work.  I'm receiving: "invalid Request  Message. No valid XML. XML must be url-encoded! maybe it contains a not  encoded ampersand or something similar"

Mentions these reuirements:
1. message sent must be UTF-8 encoded
2. Unparsed XML Data, starts with “<![CDATA[“ and ends with “]]>
3. The content-type of your message must be set to:application/x-www-form-urlencoded;charset=UTF-8

Doesn't  mention any headers so I'm really unsure what headers to send or not to  send and it's asking for the body to be urlencoded so i'm wrapping it  in URLEncodedFormat() function, have tried with or without it.

The body is not getting through for some reason, done it with <cfhttpparam type="body" and <cfhttpparam type="xml", none works....

So part of the xml the data is:


<cfsilent> 
        <cfsavecontent variable="xmlInfo">
        <cfoutput>
      
            <?xml version="1.0" encoding="UTF-8"?>
            <Request version="1.0" responseURL="#responseURL#">
           <Transaction mode="#TransactionMode#" requestTimestamp="#DateFormat(NOW(),"yyyy-mm-dd hh:mm:ss")#">
            <Identification>
                <OrderID>12345678</OrderID>
                <UUID>x65m432n210o987-20101004-1346</UUID>
            </Identification>
   
        </cfoutput>
        </cfsavecontent>

</cfsilent>

THen I have:
     <cfhttp url="#urlAddress#" method="post"  username="#httpUserName#" password="#httpPassword#" throwonerror="yes"  port="443" result="TheResult"  charset="utf-8"   useragent="cgi.HTTP_USER_AGENT">
        
         <cfhttpparam type="header" name="Content-type" value="application/x-www-form-urlencoded;charset=UTF-8">
         <cfhttpparam type="header" name="Content-length" value="#Len(Trim(transInfo))#">
            
             <cfhttpparam type="header" name="charset" value="utf-8">
             <cfhttpparam type="Header" name="TE" value="deflate;q=0">
             <cfhttpparam type="header" name="Accept-Encoding" value="*" />
             <cfhttpparam type="Header" name="TE" value="deflate;q=0">
             <cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
             <cfhttpparam type="header" name="accept-encoding" value="no-compression" />
        
             <cfhttpparam type="header" name="Document-type" value="Request">
        
             <cfhttpparam type="body" value="#URLEncodedFormat(xmlInfo,"utf-8")#">
             
         </cfhttp>

Any suggestions would be greatly appreciated.
CHeers

    This topic has been closed for replies.

    2 replies

    Inspiring
    January 26, 2011

    Comparing your code to the requirements, I think you're missing something:


    2. Unparsed XML Data, starts with “<![CDATA[“ and ends with “]]>


            <cfsavecontent variable="xmlInfo">

        
                 <cfhttpparam type="body" value="#URLEncodedFormat(xmlInfo,"utf-8")#">

    As far as I can tell the value you're passing into body is not surrounded in CDATA tags.  And urlEncodedFormat() is inappropriate here, you want xmlFormat() if you want anything, but you'd not want to xmlFormat() the entire XML string... you'd just want to xmlFormat() any text values - ones that could potentially have illegal characters in them.

    --

    Adam

    AkosFAuthor
    Known Participant
    January 27, 2011

    Thanks for that, IsXML() returns YES and I'm surrounding each element like <![CDATA[<AcquirerBIN>4111111</AcquirerBIN>]]>  and still getting:
    ' No valid XML. XML must be url-encoded! maybe it contains a not encoded ampersand or something similar'

    have tried to send using both <cfhttpparam type="body" value="#ToString(xmlData)#"> and <cfhttpparam type="xml" value="#ToString(xmlData)#">

    also tried with or without ToString and also tried with and without XMLFormat() and still no luck,
    any other suggestions would be great thanks

    Inspiring
    January 28, 2011

    Offline on a train @ the mo' so cannae check, but didn't the instructions say to wrap the •document• in CDATA tags, not each element?

    Have you been in touch with whoever provides this web service and asked then what you're doing wrong? That'd probably be the quickest solution...

    --

    Adam

    Owainnorth
    Inspiring
    January 25, 2011

    Try testing your XML with isXML() first before you worry about the webservice, see what CF makes of it.

    AkosFAuthor
    Known Participant
    January 25, 2011

    Thank you Owain, and yes you're right IsXML returns NO, however, the actual XML data structure I'm using is the one actually provided by the API documentation and I haven't modified it at all and can see no problems visually.
    Any suggestion on what I could do next to test the XML data? Or have you encountered similar issue before?
    THanks

    Owainnorth
    Inspiring
    January 26, 2011

    Do they have a full sample XML document you can just copy/paste into a cfsavecontent block and run isXml() against it?

    If their example is invalid you're not going to get anywhere. I'd suggest hard-coding in their example and testing it. If it's valid then start substituting in your own values. You might also find (although it's a bit of a pain in the ass for simple docs) that using the proper ColdFusion XML functions to create you a document rather than using plain text yields better results.


    That's where I'd start anyway

    O.