[英]How to create user in openfire using restapi in nodeJs application?
這是我在Openfire中用來創建用戶的NodeJs應用程序中的功能。
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 ,有人可以告訴我我在做什么錯嗎? 提前致謝。
我認為您提供的選項在發送前需要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)
}
我發現問題出在request-promise上 。 它未按要求的格式正確發送數據。 所以不是現在,我使用的是其他模塊minimum-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); }); };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.