node.jsexpresscoldfusioncfhttp

Porting legacy ColdFusion code to Node.js - cfhttp/request.js


I am primary a Coldfusion developer, for over 10+ years but its time to make a change. I am working through porting some old Colfusion 9 code to node js and I am struggling getting connected to the 3rd party API to access data for our company.

This is the current Coldfusion code that is connecting to the external service:

<cfsavecontent variable="thiscontent">
        <post>
            <username>username@domain.com</username>
            <password>Pa$$w0rd</password>
        </post>
</cfsavecontent>

<cfhttp url="https://API.ENDPOINT" method="post" result="httpResponse" >
  <cfhttpparam type="FormField" name="xml" value="#Trim(thiscontent)#" />
</cfhttp>

This code works find, and returns the expected XML object from the service. However, this what is interesting is that if I remove the 'method="post"' perameter, I get the same error I do when trying to connect with node, more on this in a second.

For node, I am using express.js to interact with the endpoint. Here is the code I am using:

reqOpts = {
        url: 'http://API.ENDPOINT',
        method: 'post',
        headers: {
            'Content-Type': 'application/xml'
        },
        body: '<post><username>user@domain.com</username><password>Pa44w0rd</password></post>'
    }
    var getNew = request(reqOpts, function(err, resp, body){
        console.log(body)
    }) ;

This then return the following error:

<?xml version="1.0"?>
<response><status>FAILURE</status><message>No XML string passed</message></response>

Remember when I said that removing the post parameter from cfhttp causes the same error? I cant seem to get this to work at all in node.

I have tried using request().form, request().auth etc with no success, always the same NO XML STRING PASSED error.

I would be very grateful for any assistance.


Solution

  • In your ColdFusion code you used a FormField named xml.

    Do the same in Node.js instead of putting the XML straight in the request body:

    reqOpts = {
        url: 'http://API.ENDPOINT',
        method: 'post',
        headers: {
            'Content-Type': 'application/xml'
        },
        form: {
            xml: '<post><username>user@domain.com</username><password>Pa44w0rd</password></post>'
        }
    }
    var getNew = request(reqOpts, function(err, resp, body) {
        console.log(body)
    }) ;