问题:我无法通过从Node.js发送请求来使ColdFusion REST服务创建数据源。每次我尝试将数据从Node.js发布到我的ColdFusion休息服务时,我的参数值都是空的。
从Node.js发布数据的代码
var http = require('http'),
postData = {
name: "ab"
},
postData = JSON.stringify(postData),
htOptions = {
hostname: 'localhost',
port: 8500,
path: "/rest/createdsn/dsnComponent/createDataSource",
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postdata)
}
}
var req = http.request(htOptions);
console.log(postData) // {"name":"ab"}
req.write(postData);
req.end();
req.on('response', (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.')
})
console.log('api successfully called');
});
ColdFusion REST服务代码
<cfcomponent rest="true" restPath="/dsnComponent">
<cffunction name="createDataSource" access="remote"
returnType="any" httpMethod="POST" restpath="{strDSN}" produces="application/JSON">
<cfargument name="strDSN" default="" type="any" restargsource="form">
<cfdump var = #strDSN# format="html" output="D:\api.html">
<cfset reqDSN = deserializeJSON(strDSN)>
<cfset structDSN = structNew() >
<cfset structDSN.driver = "MSSQLServer" >
<cfset structDSN.name = reqDSN.name >
<cfset structDSN.host = "localhost" >
<cfset structDSN.port = "1433" >
<cfset structDSN.database = reqDSN.name>
<cfset structDSN.username = "sa" >
<cfset structDSN.password = "*****" >
<cfscript>
adminObj = createObject("component","cfide.adminapi.administrator");
adminObj = adminObj.login('***','admin');
myObj = createObject("component","cfide.adminapi.datasource");
myObj.setMSSQL(argumentCollection = structDSN);
structDSN.success = myObj.verifyDSN(dsn = structDSN.name, returnMsgOnError = 'true');
</cfscript>
<cfset res = serializeJSON(structDSN) />
<cfreturn structDSN>
</cffunction>
</cfcomponent>
当我尝试在变量headers
中包含htOptions
选项时,我收到以下错误
如果我不包含标题,我的参数中会出现一个空字符串并收到错误
{"Message":"JSON parsing failure: Unexpected end of JSON string"}
我创建了一个cfm页面来调用其余服务,并在ColdFusion管理员中成功创建了数据源
代码
<cfset structDSN.name = "ab">
<cfset strDSN = serializeJSON(structDSN)>
<cfhttp url="http://localhost:8500/rest/createdsn/dsnComponent/createDataSource" method="post" port="8500" result="res">
<cfhttpparam name="strDSN" value="#strDSN#" type="formfield" />
</cfhttp>
我似乎无法弄清楚我做错了什么。
答案 0 :(得分:2)
在Node.JS中,您使用的是GROUP BY
标头,但是您没有正确编码内容类型的数据。
另外,我看到你希望SELECT a.ID, a.orderdate, SUM(b.apple) apple
FROM testfinal3 a
JOIN testfinal3 b ON b.orderdate <= a.orderdate AND a.ID = b.ID
GROUP BY a.ID, a.orderdate;
参数超出请求的形式,但是它没有在其中定义(除非我弄错了;我不太了解CF)。
获取表单数据时,您的请求正文字符串包含'Content-Type': 'application/x-www-form-urlencoded'
个分隔的strDSN
对,因此它应该看起来像;
。
所以,要么你做
argument=value
将产生strDNS=URLEncodedData
(所有postData = {
name: "ab"
};
postData = querystring.stringify({strDSN: JSON.stringify(postData)}); //urlencode strDSN argument as JSON string
req.write(postData); // or req.end(postData)
,strDSN=%7B%22name%22%3A+%22ab%22%7D
,{
,}
和:
均为网址编码)
见https://nodejs.org/dist/latest-v6.x/docs/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options
或者您使用"
和
(space)
然后您可能需要"Content-Type": "application/json"
CF中的请求正文/内容。
答案 1 :(得分:0)
我不确定这是否是你的问题Dan但你的函数CreateDataSource()确实存在语法错误。
你的return var设置如下:
<cfset res = serializeJSON(structDSN) />
<cfreturn structDSN>
您正在返回CF结构而不是您的&#34; res&#34;变量。那是你要的吗?
不应该是:
<cfreturn serializeJSON(structDSN)>