我正在尝试从nodejs调用restlet;这是一个简单的帖子,有一些授权,但我得到的只是一些错误:
{“错误”:{“代码”:“SYNTAX_ERROR”,“消息”:“语法错误:空 JSON 字符串(null$lib#3)。”}}
这是我的代码
request.post({
url: netSuiteUrl
headers: {
'Content-Type': 'application/json'
'Authorization': 'NLAuth nlauth_account=<account>, nlauth_email=<email>, nlauth_signature=<username>, nlauth_role=3'
}
content: '{"data":"test"}'
}, (error, response, body) ->
console.log(body)
)
restlet实际收到调用并且授权正在工作。我发送的相同数据可以在以下 PHP 代码中正常工作:
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Authorization: NLAuth nlauth_account=<account>, nlauth_email=<email>, nlauth_signature=<signature>, nlauth_role=3\r\n" .
"Content-Type: application/json\r\n",
'content': '{"data":"test"}'
)
);
$context = stream_context_create($opts);
$file = file_get_contents($url, false, $context);
有什么提示吗?
您使用哪个图书馆来发送请求?你尝试使用 Node JS 的模块
http
吗?您的内容似乎未在请求中发送。
参见下面的代码示例:
var postData = querystring.stringify({data:'test'});
var options = {
hostname: '<my host>',
port: 80,
path: '/<mypath>',
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Authorization': 'NLAuth nlauth_account=<account>, nlauth_email=<email>, nlauth_signature=<username>, nlauth_role=3',
}
};
var req = http.request(options, function(res) {
// Handle successful request
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
// Handle errors
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// Send data
req.write(postData);
req.end();
希望对您有帮助, 蒂埃里
您可以在此处查看我的 NodeJS 实现和操作方法:https://github.com/gmihaylov/netsuite-nodejs-reslet-call
希望能有所帮助:)