How to lookup vehicle information based on vehicle ID number

Sample code is provided by the US Dept of Transportation at their website at:

https://vpic.nhtsa.dot.gov/api/Home/Ind … geExamples
https://vpic.nhtsa.dot.gov/api/

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.

Dean Westover
Choices Software, Inc.

Hi Dean,

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());
	}
}

There is also a Servoy webinar about consuming REST webservices: https://www.youtube.com/watch?v=m_2cR4lpxOs

Best,

Thank you for your quick reply Danny.

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””

Dean

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.

There is a plugin in 6.x you can use to substitute the JSON library. (wiki: https://wiki.servoy.com/display/Serv60/serialize)

         // convert the JSON string into a JavaScript object
         var objectResult = plugins.serialize.fromJSON(response.getResponseBody());

The rest of the sample code should be the same.

Best,

Another alternative that I’ve used instead of plugins.serialize.fromJSON() is JSON.parse(). The following is an example:

function TestURL() {
	try {
		plugins.svyBlockUI.show('Processing Request...');
		var c = plugins.http.createNewHttpClient();
		var url = 'https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/5UXWX7C5*BA?format=json';
		var req = c.createGetRequest(url);
		req.addHeader('Content-Type', 'application/json');
		req.addHeader('Accept', 'application/json');
		var e = req.executeRequest();
		if (e.getStatusCode() == plugins.http.HTTP_STATUS.SC_OK) {
			var resp = JSON.parse(e.getResponseBody());
			for (var i = 0; i < resp['Results'].length; i++) {
				var v = resp['Results'][i];
		        application.output('Year: ' + v['ModelYear']);
		        application.output('Make: ' + v['Make']);
		        application.output('Model: ' + v['Model']);
			}
	    }
	} catch(e) {
		plugins.dialogs.showErrorDialog('Error', e.toString());
	} finally {
		plugins.svyBlockUI.stop();
	}
}