簡體   English   中英

如何在node.js中向Apache Syncope API提交POST請求?

[英]How to submit a POST request to Apache Syncope API in node.js?

薩拉姆(意思是你好):)

在我的node.js應用程序中,我試圖通過RESTful API將用戶添加到Apache Syncope。 Apache Syncope已安裝在遠程VPS上,並且本文在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));
}

Apache Syncope的響應是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>

Node.js將使用分塊傳輸編碼來發送請求,這是HTTP 1.1的一部分。 在Node.js中解決這個問題既不可取,快速或優雅。 常見的解決方案是通過nginx代理不兼容的服務(它將重新編碼請求)。 這是用於反向復制的非常簡單的示例配置。

server {
    listen 80;
    server_name syncope.example.com;
    proxy_pass http://example.com:8080/;
    allow 127.0.0.1; # Allow local connections, can be any IP(+CIDR) too
    deny all;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM