我的代码尝试将数据从本地 Node.js 服务器发布到 Coldfusion API。我已成功与 API 进行通信并通过请求标头对自己进行身份验证。然而,我在实际传递 JSON 对象时遇到了困难,因为我无法获得正确的结构。
API 不接受请求模块的 JSON 选项,因此这是我最简单的选择。
API 期望以下内容:
{
'source': {
'customer': {
'customerlogin': 'myusername',
'customerpassword': 'mypassword',
}
}
}
如果我将以下正文参数(来自其他人的成功帖子)硬编码到我的帖子中,我的代码就可以工作。
var Jrequest = require('request');
var options = {
uri: 'http://myAPI/customerSSO.json',
headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': something', 'Timestamp': timestamp},
method: 'POST',
body: 'source=%7B%0D%0A++%22customer%22%3A+%7B%0D%0A++++%22customerlogin%22%3A+%22myusername%22%2C%0D%0A++++%22customerpassword%22%3A+%22mypassword%22%2C%0D%0A%09%22success%22%3A+%22%22%0D%0A++%7D%0D%0A%7D' // Working
};
Jrequest(options, function (error, response, body){
res.send(body);
});
如果我以其他方式发送 JSON,例如 json.stringify(),它会被拒绝,理由是“源是必需的,但未定义”。
所以我想我的问题是,在 Node.js 中如何将 JSON 转换为如下所示的内容
'source=%7B%0D%0A++%22customer%22%3A+%7B%0D%0A++++%22customerlogin%22%3A+%22myusername%22%2C%0D%0A++++%22customerpassword%22%3A+%22mypassword%22%2C%0D%0A%09%22success%22%3A+%22%22%0D%0A++%7D%0D%0A%7D'
或者我忽略了另一个选择?
感谢您的帮助,如果我使用了不正确的术语,请道歉。