简体   繁体   English

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

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

Salam (means Hello) :) 萨拉姆(意思是你好):)

In my node.js application, I am trying to add a user to Apache Syncope via its RESTful API. 在我的node.js应用程序中,我试图通过RESTful API将用户添加到Apache Syncope。 Apache Syncope is installed on a remote VPS and this article demonstrate this process in a PHP environment. Apache Syncope已安装在远程VPS上,并且本文在PHP环境中演示了此过程。 here is what I did in my node.js application: 这是我在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));
}

The response of Apache Syncope is a 415 error (css styles omitted): 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 will use chuncked transfer encoding for sending requests, which is part of HTTP 1.1. Node.js将使用分块传输编码来发送请求,这是HTTP 1.1的一部分。 Getting around this inside Node.js is neither desireable, fast or elegant. 在Node.js中解决这个问题既不可取,快速或优雅。 A common solution is to proxy the incompatible service though nginx (which will re-encode the request). 常见的解决方案是通过nginx代理不兼容的服务(它将重新编码请求)。 Here's a very simple example configuration for reverse-proxing. 这是用于反向复制的非常简单的示例配置。

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