从Node调用NetSuite restlet

时间:2015-03-12 10:13:26

标签: node.js restlet netsuite

我试图从nodejs调用restlet;它是一个带有一些授权的简单帖子,但我得到的只是一些错误:

{"错误" :{"代码" :" SYNTAX_ERROR"," message" :" SyntaxError:清空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);

任何提示?

1 个答案:

答案 0 :(得分:0)

您使用哪个图书馆发送请求?您是否尝试使用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();

希望它可以帮到你, 亨利