REST API Connection Problem

Hi Experts,

I am new in OPENFIRE and I need help in using REST API.

I manage to install OPENFIRE server successfully and create a new user from the admin console. However, when I try to create a new user by using the REST API, I failed. I have been trying to call the methods from REST API for few days using JQuery, Ajax, and Javascripts. But none of the codes manage to create a new user. I desperately need some helps from the experts. Thanks!!! These are some of my codes, I hope someone can point out my errors. Thanks again!!!

functionuserCreation() {

varxmlhttp;

xmlhttp = new XMLHttpRequest();

xmlhttp.open('POST', '[http://localhost:9090/plugins/restapi/v1/users](http://localhost:9090/plugins/restapi/v1/users)', true);

xmlhttp.setRequestHeader(‘Authorization’, ‘G4diPwo5’);

xmlhttp.setRequestHeader(“Content-Type”, “application/xml”);

var data =

“<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>” +

“” +

“test6” +

“p4ssword” +

“”;

xmlhttp.onreadystatechange = function() {

if (xmlhttp.status == 201) {

alert(“Update Successfully”);

}

}

xmlhttp.send(data);

}

function userCreation2() {

jQuery.ajax({

headers: {

“Authorization”: “G4diPwo5”,

},

type: ‘POST’,

contentType: “application/json; charset=utf-8”,

dataType: “json”,

        url: '[http://localhost:9090/plugins/restapi/v1/users](http://localhost:9090/plugins/restapi/v1/users)',

data: {

“username”: “test6”,

“password”: “p4ssword”

},

success: function() {

alert(‘POST completed’);

},

error: function(xhr, status, error){

alert(xhr);

var err = eval("(" + xhr.responseText + “)”);

alert(err.Message);

}

})

}

What is the response message/http status code do you get?

I would also suggest to use an php REST API Client: gidkom/php-openfire-restapi · GitHub

Thanks for your reply. I use

alert(xmlhttp.responseText);

alert(xmlhttp.statusText);

alert(xmlhttp.responseType);

alert(xmlhttp.responseXML);

to print out the response. However the response is an empty string and xmlhttp.responseXML give null. Is that any way that I can use to test whether the REST API plugin is ready to use. I have gone through the php scripts also, but I am using glassfish server now and I cannot deploy the php-script to the server. If there were no other ways to test the REST API, I will install the xampp server and use the script to test REST API plugin. And please share with me if there were plugins that I can use to monitor the request that has been sent to the API? Your help is greatly appreciated.

Thank you !!!

You could use Postman an Chrome Extension to test your REST calls: Postman - Chrome Web Store

I guess your authentication is wrong.

I used Postman to test the api to create a user. But I am getting unauthorized 401 error. I passed basic authentication in header…

Can u suggest something.

Did you read this: REST API Plugin Readme

Could you provide your request (with all headers)

And did you configured the authentication in Openfire?

yes, i read that documentation and here is my request headers at

http://127.0.0.1:9090/plugins/restapi/v1/users

Authorization: Basic YWRtaW46MTIzNDU2 (admin, 123456)

Content-Type : application/json

Capture.PNG

Above is the data i m passing to create the user.

i m not sure about your last point “**And did you configured the authentication in Openfire?” **how can i check this

Hey,

you need to configure the type of the authentication in Openfire.

Look here: https://imagestack.net/i/0fc89cc036ad.jpg

Another issue is, that you send your payload as “x-www.form-urlencoded”.

But you should send it in XML or JSON format.

from poatman it is working now but through ajax still is giving me error.

Do you get 400 response status or the 401?

400 means your sent data is corrupt / wrong formated. 401 means your authorization is wrong.

i got 401… i tried from different machine and

It is because of CORS. i enabled it on server but still getting. can u please help me in this. how to exactly configure CORS.

I guess that REST API don’t support CORS at the moment.

you can try below: i opened it for public as of now and correct where i m doing wrong.

$.ajax({

                        url: 'http://52.26.189.80:9090/plugins/restapi/v1/users',

type: ‘GET’,

dataType: ‘json’,

contentType: ‘application/json’,

headers: { ‘Authorization’: ‘Basic YWRtaW46c2V0dXAyOTUh’ },

success: function (json) {

console.log(“Test”);

},

error: function (err) {

}

});

then how to make it supportable.

The REST API Plugin need to be extended for that. Is it mandatory to use ajax on your side? can the call not be done with PHP or something like that?

I am creating cross platform app with Ionic framework Ionic: Advanced HTML5 Hybrid Mobile App Framework . So need to use Ajax with this case.

I am creating cross platform app with Ionic framework. So need to use Ajax with this case.

I am worried it is working through postman. no need of ajax

Yes, authentication is enabled as It is as you shown in image.

Even get is not working… we can look at payload later on. I am amazed how POSTMAN is doing it.

It probably due to CORS.

$.ajax({

        url: 'http://52.26.189.80:9090/plugins/restapi/v1/users',

method: ‘GET’,

dataType: ‘json’,

contentType: ‘application/json’,

headers: { ‘Authorization’: ‘Basic YWRtaW46c2V0dXAyOTUh’ },

success: function (json) {

console.log(“Test”);

},

error: function (err) {

}

});

Its definitely because of CORS. The reason is that you try to call from javascript which need CORS if you don’t call it from the same server.

Then what is the solution… this is exhausting me now. I tried too many options.