2017-10-09 88 views
0

我主要一個ColdFusion開發,已有10多年,但它的時間來做出改變。我正在通過將一些舊的Colfusion 9代碼移植到節點js上工作,並且我正在努力連接到第三方API來訪問我們公司的數據。移植遺留的ColdFusion代碼的Node.js - CFHTTP/request.js

這是連接到外部服務的當前ColdFusion代碼:

<cfsavecontent variable="thiscontent"> 
     <post> 
      <username>[email protected]</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> 

此代碼查找,並返回該服務的預期XML對象。然而,有趣的是,如果我刪除了'method =「post」'參數,我在嘗試連接節點時遇到了同樣的錯誤,這在一秒鐘內就會發生。

爲節點,我使用express.js與端點交互。這裏是代碼我使用:

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

這則返回以下錯誤:

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

還記得我說過,從CFHTTP移除之後的參數會導致同樣的錯誤?我似乎無法得到這個在節點工作。

我已經使用請求()。形式,請求試圖AUTH等()。沒有成功,總是相同的NO XML字符串傳遞錯誤。

我將非常感謝您的幫助。

回答

1

在你的ColdFusion代碼你用了一個名爲xml FormField。

做相同的Node.js的,而不是把XML直接在請求體的:

reqOpts = { 
    url: 'http://API.ENDPOINT', 
    method: 'post', 
    headers: { 
     'Content-Type': 'application/xml' 
    }, 
    form: { 
     xml: '<post><username>[email protected]</username><password>Pa44w0rd</password></post>' 
    } 
} 
var getNew = request(reqOpts, function(err, resp, body) { 
    console.log(body) 
}) ; 
+0

嗯,就這麼簡單。我不知道你可以在節點中命名這樣的表單,非常感謝你指向正確的方向,我可以訪問數據。 –