SambaPOS API Integration with NewBook PMS/Booking System

So am I understanding right with the new web.PostData in theory my code will look something like;

function JSONTest()
{
 var url = 'https://sambapos:xxxxxxxxxxxxxxxxxxxxxxx@testau.newbookpms.com/rest/auth_test';
 var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxxxxxx\"}';
 var response = web.PostData(url,api);
 var responseObject = JSON.parse(response);
 return responseObject.json.success;
}

if the expected post result options were;

{"success":"You have successfully authenticated to NewBook Test Property"}

Which with any luck would give result of

You have successfully authenticated to NewBook Test Property

There are three error outputs I have seen so far which are;

{"error":"Authentication Required"}
{"error":"API Key not provided"}
{"error":"Could not decode JSON data\n<!-- XXXXXXXXXXXXXXXXXX -->"}

so if not expected result or no result changine success to error should show the error message reason?


I wasnt expecting it to work but thought I would try
 if I use the web.Upload as below;

function JSONTest()
{
 var url = 'https://sambapos:xxxxxxxxxxxxxxxxx@testau.newbookpms.com/rest/auth_test';
 var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxxx\"}';
 var response = web.Upload(url,api);
 var responseObject = JSON.parse(response);
 return responseObject.json.success;
}

Test gives message of ‘json.success’ is null or not an object, hopefully this is because Upload doesn’t look for response vs PostData as sugested by QMcKay

I know what your saying but the documentation and from my understanding is you ‘POST’ the

{"api_key":"instances_xxxxxxxxxxxxxxxxxxxxxx"}

to the REST address based on the request,

The responce is JSON values.

The auth_test url will return one of the above,
If it works and returns
{“success”:“You have successfully authenticated to NewBook Test Property”}
the value for success would be You have successfully authenticated to NewBook Test Property

Either that or I have misunderstood the whole thing.

It should probably look like this:

function JSONTest()

{

 var url = 'https://sambapos:xxxxxxxxxxxxxxxxxxxxxx@testau.newbookpms.com/rest/auth_test';

 var api = '{"api_key": "xxxxxxxxxxxxxxxxxxxxxxxxxx"}';

 var response = web.Upload(url,api);

 var responseObject = JSON.parse(response);

 return responseObject.json.success;

}

It seems to not work with web.Upload though.

What have you changed? Just the api key?
I went with similar to emre with the " - presumed it was a character thing with "

yes, {“api_key”:“instances_xxxxxxxxxxxxxxxxxxxxxxxxxxx”} in custom data in RESTezey firefox plugin works.
However it has specific firelds for HTTP Basic Auth but beleive it should work with the user:password@ prefix on url

It is probably the web.Upload not working like we think it might be.

in theory this should work.

function JSONTest()

{

 var url = 'https://sambapos:xxxxxxxxxxxxxxxxxxxxxx@testau.newbookpms.com/rest/auth_test';

 var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxxx\"}';

 var response = web.PostData(url,api);

 var responseObject = JSON.parse(response);

 return responseObject.json.success;

}

That is assuming the \ were escape for "

When .48 comes out we can test that. Unless @emre wants to be nice and test it for us lol.

Was I that obvious in posting it with the PostData :blush:

It does work
 it says authentication required
 meaning it worked but it needs the key.

Two main errors ive seen are;

There is one specificly for api key and auth pops up is I put username wrong on purpose.

Where were you trying it and getting that response? Samba??

Try returning response to see if there is anything there. If that works, return resposeObject to see if anything is there.

If both of those work, then your problem lies here, which would be a syntax issue:

responseObject.json.success

The reason I say this is because these Helpers and Methods are actually case-sensitive. For example, if you do:

json.parse()

it will fail, while this works:

JSON.parse()

The issue is its not uploading the API key

1 Like

Arrrrr - PROGRESS :smile:

 return response;

Gives {"error":"Authentication Required"}

{“error”:“Authentication Required”} is issue with the user and password rather than API
If API not received it would give {“error”:“API Key not provided”}

having sambapos:xxxxxxxxxxxxxxxx@ in the url obviously isnt working for the auth

1 Like

So lets look at another avenue.

What you got in mind?

The RESTesey plugin says this above the user/password fields;

Authentication
The parameters provided below will be used for HTTP Basic Auth.

Googling HTTP Basic Auth suggested that user.password@ should work.

If I narrow the search to jscript google presumes its javascript and get solutions using ajax like;

$.ajax
({
  type: "GET",
  url: "index1.php",
  dataType: 'json',
  async: false,
  headers: {
    "Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
  },
  data: '{ "comment" }',
  success: function (){
    alert('Thanks for your comment!'); 
  }
});

So they have not enabled HTTP Basic authentication they are only using Basic Authentication. We may need a helper from Emre for this to work but i will research more.

Other thing that keeps cropping up in searching for solution is using ‘headers’

var request = require('request'),
    username = "john",
    password = "1234",
    url = "http://www.example.com",
    auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

request(
    {
        url : url,
        headers : {
            "Authorization" : auth
        }
    },
    function (error, response, body) {
        // Do more stuff with 'body' here
    }
);

I am pretty sure we need a helper to do this. Maybe @emre can shed some light on it.

Check this

And maybe you should disable this default behavior

https://support.microsoft.com/en-us/kb/834489

Well, he has mentioned in Beta, the next version will have this, so it may help


functions added to JScript:

web.PostData(<url>,<data>,[<user>],[<password>])
web.PostJson(<url>,<json>,[<user>],[<password>]) 

Both functions are identical but web.PostJson function adds "Content-Type", "application/json" header to the request.

2 Likes

There you go @JTRTech your answer right there.

1 Like