Sample code for 30+ languages & platforms
Zig

DVLA Vehicle Enquiry Service

See more _Miscellaneous_ Examples

Demonstrates how to make a call to the DVLA Vehicle Enquiry API to get vehicle details of a specified vehicle. It uses the vehicle registration number as input to search and provide details of the vehicle.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Implements the following CURL command:

    // curl -X POST https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles \
    //   -H "x-api-key: supplied API key" \
    //   -H "Accept: application/json" \
    //   -d '{"registrationNumber": "ABC1234"}'

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "registrationNumber": "ABC1234"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("registrationNumber", "ABC1234") catch {};

    http.setRequestHeader("Accept", "application/json");
    http.setRequestHeader("x-api-key", "supplied API key");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "artEndDate": "2025-02-28",
    //   "co2Emissions": 135,
    //   "colour": "BLUE",
    //   "engineCapacity": 2494,
    //   "fuelType": "PETROL",
    //   "make": "ROVER",
    //   "markedForExport": false,
    //   "monthOfFirstRegistration": "2004-12",
    //   "motStatus": "No details held by DVLA",
    //   "registrationNumber": "ABC1234",
    //   "revenueWeight": 1640,
    //   "taxDueDate": "2007-01-01",
    //   "taxStatus": "Untaxed",
    //   "typeApproval": "N1",
    //   "wheelplan": "NON STANDARD",
    //   "yearOfManufacture": 2004,
    //   "euroStatus": "EURO 6 AD",
    //   "realDrivingEmissions": "1",
    //   "dateOfLastV5CIssued": "2016-12-25"
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    _ = try j_resp.stringOf(alloc, "artEndDate");
    _ = j_resp.intOf("co2Emissions");
    _ = try j_resp.stringOf(alloc, "colour");
    _ = j_resp.intOf("engineCapacity");
    _ = try j_resp.stringOf(alloc, "fuelType");
    _ = try j_resp.stringOf(alloc, "make");
    _ = j_resp.boolOf("markedForExport");
    _ = try j_resp.stringOf(alloc, "monthOfFirstRegistration");
    _ = try j_resp.stringOf(alloc, "motStatus");
    _ = try j_resp.stringOf(alloc, "registrationNumber");
    _ = j_resp.intOf("revenueWeight");
    _ = try j_resp.stringOf(alloc, "taxDueDate");
    _ = try j_resp.stringOf(alloc, "taxStatus");
    _ = try j_resp.stringOf(alloc, "typeApproval");
    _ = try j_resp.stringOf(alloc, "wheelplan");
    _ = j_resp.intOf("yearOfManufacture");
    _ = try j_resp.stringOf(alloc, "euroStatus");
    _ = try j_resp.stringOf(alloc, "realDrivingEmissions");
    _ = try j_resp.stringOf(alloc, "dateOfLastV5CIssued");
}