0

This is my function in NodeJs app which I am using to create user in 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);
        });
};

the objToSave is a json object contains username and password.

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

when i run this function i am getting the following error..

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

I configured my secret-key properly and domain name is localhost://9090, can anybody tell me what I am doing wrong ? thanks in advance.

Kunal Pal
  • 545
  • 8
  • 21

2 Answers2

0

I think the options you provided needs JSON.stringify object before send it

The modified options is as below

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)
  }
yue you
  • 2,206
  • 1
  • 12
  • 29
  • No, It's not working. @yue you. still getting the same error. – Kunal Pal Feb 05 '18 at 12:04
  • try change your json to `{ "username": "admin", "password": "p4ssword" }` as the doc mentioned https://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#create-a-user – yue you Feb 05 '18 at 12:08
  • Still no progress, I changed into above format but got the same error. – Kunal Pal Feb 05 '18 at 12:17
  • really. Maybe you can use some code irrelevant toolkit like postman to test api endpoint availability first – yue you Feb 05 '18 at 12:34
0

I find out that problem was with request-promise. it was not properly sending data in the required format. so Instead of that now I am using different module minimal-request-promise. and It worked like charm for me. After using that, my code looks something like this.

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);
        });
};
Kunal Pal
  • 545
  • 8
  • 21