I suspect that either their “post” or “javascript” example could be used, but I am not certain about the exact syntax required by Servoy. We would like to have our user be able to type in a vehicle identification number and press a button to have the year, make, model, type, etc. automatically populate fields with similar names in our Servoy webclient solution. This would both speed up data input and improve accuracy.
Any assistance would be greatly appreciated. This is for a Servoy version 6.x webcient solution.
Below there is a sample code you can use, tested in latest version of Servoy but the HTTP plugin should work nearly the same in 6.x (wiki: https://wiki.servoy.com/display/Serv60/http)
function searchVIN() {
// 'vin' can be a form variable
var url = 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevinvalues/' + vin + '?format=json';
var client = plugins.http.createNewHttpClient();
// not sure if 'encodeURI' is present in 6.x, but if not you can use the 'url' variable without encoding as well
var request = client.createGetRequest(encodeURI(url));
var response = request.executeRequest();
if (!response) {
plugins.dialogs.showErrorDialog('Error', 'No response received from server');
return;
}
if (response.getStatusCode() == plugins.http.HTTP_STATUS.SC_OK) {
try {
// convert the JSON string into a JavaScript object
var objectResult = JSON.parse(response.getResponseBody());
// it looks like there is always an array under 'Results' with at least one item
var result = objectResult['Results'][0];
// year, make, and model taken from examining the formatted JSON string
application.output('Year: ' + result['ModelYear']);
application.output('Make: ' + result['Make']);
application.output('Model: ' + result['Model']);
if (result['ErrorText']) {
application.output('Error: ' + result['ErrorText']);
}
}
catch (e) {
plugins.dialogs.showErrorDialog('Error', 'Invalid json response received from server');
}
}
else {
plugins.dialogs.showErrorDialog('Error', 'HTTP error code received from server: ' + response.getStatusCode());
}
}
The code within Servoy developer shows two lines with error messages:
“The property HTTP_STATUS is undefined for the type Plugin ”
and
“Reference is undeclared variable of property of JSON”
When I make the code a button action and click the button in webclient, in the Developer console I see:
“TypeError: Cannot read property “SC_OK””
According to the wiki documentation for 6.x HTTP_STATUS should exist in the http plugin, in any case, SC_OK is equal to 200 so you can compare to that.
About JSON, I am not sure if it’s available in 6.x, in case not you might need a library that implements the parser.