How to lookup vehicle information based on vehicle ID number

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

How to lookup vehicle information based on vehicle ID number

Postby Westy » Tue Aug 11, 2020 7:44 pm

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.
Westy
 
Posts: 852
Joined: Fri Feb 13, 2004 5:27 am
Location: Lynnfield, Massachusetts USA

Re: How to lookup vehicle information based on vehicle ID nu

Postby dcamargo » Tue Aug 11, 2020 8:49 pm

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)
Code: Select all
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,
Danny Camargo
Servoy & Web Developer
dcamargo
 
Posts: 13
Joined: Tue Aug 23, 2016 8:59 pm

Re: How to lookup vehicle information based on vehicle ID nu

Postby Westy » Tue Aug 11, 2020 11:27 pm

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 <http>"
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
Westy
 
Posts: 852
Joined: Fri Feb 13, 2004 5:27 am
Location: Lynnfield, Massachusetts USA

Re: How to lookup vehicle information based on vehicle ID nu

Postby dcamargo » Wed Aug 12, 2020 2:57 pm

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.
Danny Camargo
Servoy & Web Developer
dcamargo
 
Posts: 13
Joined: Tue Aug 23, 2016 8:59 pm

Re: How to lookup vehicle information based on vehicle ID nu

Postby dcamargo » Wed Aug 12, 2020 3:36 pm

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

Code: Select all
         // 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,
Danny Camargo
Servoy & Web Developer
dcamargo
 
Posts: 13
Joined: Tue Aug 23, 2016 8:59 pm

Re: How to lookup vehicle information based on vehicle ID nu

Postby Louis.Winter » Thu Aug 13, 2020 9:06 pm

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

Code: Select all
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();
   }
}
Louis Winter
Journey Technology Solutions
Sioux Falls, SD USA
Louis.Winter
 
Posts: 24
Joined: Thu Dec 13, 2018 8:10 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 7 guests

cron