【问题标题】:How to create user in openfire using restapi in nodeJs application?如何在 node Js 应用程序中使用 rest api 在 openfire 中创建用户?
【发布时间】:2018-07-15 06:19:02
【问题描述】:

这是我在 NodeJs 应用程序中的函数,我用它在 openfire 中创建用户。

var createUser = function(objToSave, callback) {
    const options = {
        method: 'POST',
        uri: url.resolve(Config.APP_CONSTANTS.CHAT_SERVER.DOMAIN_NAME, '/plugins/restapi/v1/users'),
        headers: {
            'User-Agent': 'Request-Promise',
            'Authorization': Config.APP_CONSTANTS.CHAT_SERVER.SECRET_KEY,
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        data: objToSave
    }
    request(options)
        .then(function(response) {
            callback(null, response);
        })
        .catch(function(error) {
            // Deal with the error
            console.log(error);
            callback(error);
        });
};

objToSave是一个json对象,包含用户名和密码。

{
  "Username": "gabbar",
  "Password": "gabbar@123"
}  

当我运行这个函数时,我收到以下错误..

{
  "statusCode": 400,
  "error": "Bad Request"
}

我正确配置了我的密钥,域名是 localhost://9090,谁能告诉我我做错了什么?提前致谢。

【问题讨论】:

    标签: javascript node.js rest openfire node-rest-client


    【解决方案1】:

    我认为您提供的选项在发送之前需要JSON.stringify 对象

    修改后的选项如下

    const options = {
            method: 'POST',
            uri: url.resolve(Config.APP_CONSTANTS.CHAT_SERVER.DOMAIN_NAME, '/plugins/restapi/v1/users'),
            headers: {
                'User-Agent': 'Request-Promise',
                'Authorization': Config.APP_CONSTANTS.CHAT_SERVER.SECRET_KEY,
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            data: JSON.stringify(objToSave)
      }
    

    【讨论】:

    • 不,它不起作用。 @悦你。仍然出现同样的错误。
    • 尝试将您的 json 更改为 { "username": "admin", "password": "p4ssword" },正如文档中提到的 igniterealtime.org/projects/openfire/plugins/restapi/…
    • 还是没有进展,我改成上面的格式还是一样的错误。
    • 真的。也许你可以先使用一些代码无关的工具包,比如 postman 来测试 api 端点的可用性
    【解决方案2】:

    我发现问题出在 request-promise 上。它没有以所需的格式正确发送数据。所以现在我使用不同的模块 minimal-request-promise 而不是那个。它对我来说就像魅力一样。使用后,我的代码看起来像这样。

    var requestPromise = require('minimal-request-promise');
    
    var createUser = function(objToSave, callback) {
        const options = {
            headers: {
                'Authorization': Config.APP_CONSTANTS.CHAT_SERVER.SECRET_KEY,
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(objToSave)
        };
        requestPromise.post('http://localhost:9090/plugins/restapi/v1/users', options)
            .then(function(response) {
                callback(null, response);
            })
            .catch(function(error) {
                // Deal with the error
                console.log(options);
                console.log(error);
                callback(error);
            });
    };

    【讨论】:

      猜你喜欢
      • 2019-01-16
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      相关资源
      最近更新 更多