Home > Uncategorized > Call a JSON-based #API with basic authentication using #Dart / #Flutter

Call a JSON-based #API with basic authentication using #Dart / #Flutter



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&#8217;;
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!

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: