Call a JSON-based #API with basic authentication using #Dart / #Flutter
TL;DR;
The Github repo is here, for anyone to clone / fork : https://github.com/infiniteloopltd/DartLicensePlateAPI
What this does, is call a JSON based API, using basic authentication using the Dart programming language. The code is pretty simple, and it only requires ‘http’ as a dependency.
import ‘package:http/http.dart’ as http;
import ‘dart:convert’ as convert;Future<dynamic> LicensePlateUK(String Reg, String Username, String Password) async {
String basicAuth = ‘Basic ‘ + convert.base64Encode(convert.utf8.encode(‘$Username:$Password’));
String url = ‘https://www.regcheck.org.uk/api/json.aspx/Check/$Reg’;
var response = await http.get(url,
headers: <String, String>{‘authorization’: basicAuth});
return convert.jsonDecode(response.body);
}
Which can be consumed as follows;
import ‘package:RegCheck/RegCheck.dart’ as RegCheck;
void main(List<String> arguments) async {
// Usage:
// dart bin/main.dart *VRM* *USERNAME* *PASSWORD*
// Where *VRM* is a UK vehicle registration mark (license plate)
// *USERNAME* and *PASSWORD* are available from https://www.regcheck.org.uk
var vehicle = await RegCheck.LicensePlateUK(arguments[0],arguments[1],arguments[2]);
print(‘Description: ${vehicle[‘Description’]}’);
print(‘Engine: ${vehicle[‘EngineSize’][‘CurrentTextValue’]}’);
print(‘Fuel Type: ${vehicle[‘FuelType’][‘CurrentTextValue’]}’);
print(‘Transmission: ${vehicle[‘Transmission’][‘CurrentTextValue’]}’);
print(‘Image: ${vehicle[‘ImageUrl’]}’);
print(‘Body Style: ${vehicle[‘BodyStyle’][‘CurrentTextValue’]}’);
print(‘Colour: ${vehicle[‘Colour’]}’);
print(‘Registration Date: ${vehicle[‘RegistrationDate’]}’);
print(‘Engine Number: ${vehicle[‘EngineNumber’]}’);
print(‘VIN: ${vehicle[‘VehicleIdentificationNumber’]}’);
}
I’m going to publish a package to pub.dev once I figure out how to do that!