薩拉姆(手段你好):)如何在node.js中向Apache Syncope API提交POST請求?
在我的node.js應用程序中,我試圖通過其RESTful API向Apache Syncope添加用戶。 Apache Syncope安裝在遠程VPS上,this article在PHP環境中演示此過程。這裏是我在我的Node.js應用所做的:
http.createServer(onRequest).listen(80);
function onRequest (request, response) {
var options = {
hostname: MY_VPS_IP,
port: 8080,
path: '/syncope/rest/user/create.json',
auth: 'admin:password',
method: 'POST'
};
var data = {an: 'object', of: 'new', user: 'data' };
var req=http.request(options, function(res) {
var body;
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function() {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(body, 'utf-8');
});
});
req.end(JSON.stringify(data));
}
阿帕奇暈厥的響應是415錯誤(CSS樣式略):
undefined
<html>
<head><title>Apache Tomcat/7.0.42 - Error report</title>
</head>
<body><h1>HTTP Status 415 - </h1>
<HR size="1" noshade="noshade">
<p><b>type</b> Status report</p>
<p><b>message</b> <u></u></p>
<p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the
requested resource for the requested method.</u></p>
<HR size="1" noshade="noshade">
<h3>Apache Tomcat/7.0.42</h3></body>
</html>
節點使用chuncked傳輸編碼,這裏不支持暈渲。由於您可能已經運行了nginx,只需將它粘貼在暈厥之前即可。 – Gant
@DamonGant我對nginx不熟悉,你可以解釋一下'堅持在暈厥前面'是什麼意思嗎?謝謝。 –