Authentication API Code Examples - Authenticate
Return to Authentication API Guide here.
cURL
curl -X POST -d '{
"userName": "REPLACE WITH YOUR USERNAME",
"password": "REPLACE WITH YOUR PASSWORD"
}' https://driver-vehicle-licensing.api.gov.uk/thirdparty-access/v1/authenticate \
-H 'Content-Type: application/json' -H 'Accept: application/json'
PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://driver-vehicle-licensing.api.gov.uk/thirdparty-access/v1/authenticate",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>array(
"userName": "REPLACE WITH YOUR USERNAME",
"password": "REPLACE WITH YOUR PASSWORD"
),
CURLOPT_HTTPHEADER => "{\"Content-Type\": \"application/json\"}",
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
C# (UsingRestSharp)
var client = new RestClient("https://driver-vehicle-licensing.api.gov.uk/thirdparty-access/v1/authenticate");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\"userName\":\"REPLACE WITH YOUR USERNAME\",\"password\":\"REPLACE WITH YOUR PASSWORD\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Ruby (Using HTTParty)
https://github.com/jnunemaker/httparty
require 'httparty'
require 'json'
api_path = 'https://driver-vehicle-licensing.api.gov.uk/thirdparty-access/v1/authenticate'
headers = {"Content-Type" => "application/json"}
query = {"userName": "REPLACE WITH YOUR USERNAME", "password": "REPLACE WITH YOUR PASSWORD"}.to_json
response = HTTParty.post(api_path, headers: headers, body: query)
puts response
NodeJS (Native)
var https = require('follow-redirects').https;
var fs = require('fs');
var options = {
method: 'POST',
hostname: 'driver-vehicle-licensing.api.gov.uk',
path: '/thirdparty-access/v1/authenticate',
headers: {
'Content-Type': 'application/json',
},
maxRedirects: 20,
};
var req = https.request(options, function(res) {
var chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function(chunk) {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
res.on('error', function(error) {
console.error(error);
});
});
var postData = JSON.stringify({ username: 'REPLACE WITH YOUR USERNAME', password: 'REPLACE WITH YOUR PASSWORD' });
req.write(postData);
req.end();
NodeJS (Using Axios)
https://github.com/axios/axios
var axios = require('axios');
var data = JSON.stringify({ username: 'REPLACE WITH YOUR USERNAME', password: 'REPLACE WITH YOUR PASSWORD' });
var config = {
method: 'post',
url:
'https://driver-vehicle-licensing.api.gov.uk/thirdparty-access/v1/authenticate',
headers: {
'Content-Type': 'application/json',
},
data: data,
};
axios(config)
.then(function(response) {
console.log(JSON.stringify(response.data));
})
.catch(function(error) {
console.log(error);
});
Python (Using Requests)
https://requests.readthedocs.io/en/master/
import requests
url = "https://driver-vehicle-licensing.api.gov.uk/thirdparty-access/v1/authenticate"
payload = "{\"username\": \"REPLACE WITH YOUR USERNAME\", \"password\": \"REPLACE WITH YOUR PASSWORD\"}"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))
Java (Using HTTPClient)
https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://driver-vehicle-licensing.api.gov.uk/thirdparty-access/v1/authenticate"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"username\":\"REPLACE WITH YOUR USERNAME\", \"password\":\"REPLACE WITH YOUR PASSWORD\"}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}