SambaPOS API Integration with NewBook PMS/Booking System

You can test it from within Script design screen look at bottom right the small box. Look at Emre screenshot it shows how to type the function to test.

Which version will that be? .48 when uploaded?

No its available now.

You need to call the function. I doubt it will work anyway, since the StackOverflow article even states that JSONrequest is in draft, and not natively available to browsers, so they use jQuery library instead.

Calling your function in your HTML page is simple… just call it.

<!doctype html>
<html>
<head>
<title>TEST</title>

<script type="text/javascript">
function JSONTest()
{
requestNumber = JSONRequest.post(
    "https://sambapos:xxxxxxxxxxxxxxxxxxxxxxxxxx@testau.newbookpms.com/rest/auth_test",
    {
        "api_key":"instances_xxxxxxxxxxxxxxxxxxxxxx"
    },
    function (requestNumber, value, exception) {
        if (value) {
            processResponse(value);
        } else {
            processError(exception);
        }
    }
); 
}
</script>

</head>

<body>

<script type="text/javascript">
JSONTest();                     // call the function defined above
</script>

</body>
</html>

Sorry JTRtech the post method not until next version so it wont do you any good atm anyway. But when its released you can test it like I mentioned.

@emre, how is web.PostData() different from web.Upload() ?

Maybe PostData accepts return value while Upload does not?

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!'); 
  }
});