Authentication

The PCMiler REST service requires an API key to access the service. The API key is used either in the URL or in the HTTP request header to validate a user’s request. If the client making the API request has an invalid API key, then the key will fail to authenticate.

Request Properties

A typical REST action consists of sending an HTTP request to the PCMiler API Server and waiting for the response. Like any HTTP request, a REST request to PCMiler API Server contains a request method, a URI, request headers, and a query string or request body. The response contains an HTTP status code, response headers, and a response body.

Request Element Description
HOST The Host header defines PCMiler REST API server (where to connect).
Example: Host: pcmiler.alk.com
APIKEY API Key will be used to fully determine privileges and visibility for the request within PCMiler platform.
Example:
add Authorization: YOUR_KEY_HERE in Request Header
Or
attach &authToken= YOUR_KEY_HERE in the URL.
CONTENT-TYPE Defines expected request MIME type.
application/json: PCMiler will render the response in JSON format following the current REST API specifications.

Sample GET Requests

Java Header-Based GET Authentication

Query = "coords=-85.9747,37.8928";
String nospac = Query.replace(" ", "%20");
address = "http://pcmiler.alk.com/apis/rest/v1.0/Service.svc/locations?";
urlFormatted = address + nospac;
url = new URL(urlFormatted);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", apiKeyVal);
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setReadTimeout(15 * 1000);
connection.connect();
builder = new StringBuilder();
String type = connection.getContentType();
if (type == null) {
    return;
}
                

C# Header-Based Get Authentication

string resource = "locations";
string queryString = "?coords=-85.9747,37.8928";
Uri requestUri = new Uri(baseURL + resource + queryString);
HttpWebRequest req = WebRequest.Create(requestUri) as HttpWebRequest;
req.Headers["Authorization"] = apiKey.Text;
req.ContentType = "application/json";
 
try
{
    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
    {
        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            txtResponse.Text = sr.ReadToEnd();
        }
    }
}
                

Javascript Header-Based GET Authentication

reqhttp = new XMLHttpRequest();
var url = "http://pcmiler.alk.com/APIs/REST/v1.0/Service.svc/";
var resource = "locations"
var queryString = "?coords=-85.9747,37.8928";
url = (url + resource + queryString);
reqhttp.open("GET", url, true);
reqhttp.onreadystatechange = ProcessRequest;
function ProcessRequest() {
    if (reqhttp.readyState == 4 && reqhttp.status == 200) {
    TextArea1.value = "Request: " + url + "\n\n";
    TextArea1.value = TextArea1.value + "Response: " + reqhttp.responseText;
    }
}
reqhttp.setRequestHeader("Content-type", "application/json");
reqhttp.setRequestHeader("Authorization", apiKeyVal);
reqhttp.responseType = "application/json";
reqhttp.send();    
                

Sample POST Requests

Jave POST Request

address = ("http://pcmiler.alk.com/APIs/REST/v1.0/Service.svc/mapRoutes?dataset=Current");
url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
apiKeyVal = apiKey.getText();
conn.setRequestProperty("Authorization", apiKeyVal);            
queryStr.setText("{ MyPostQuery }");
String input = queryStr.getText();
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != 200)
    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {    
        throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode());
    }
String type = conn.getContentType();
if (type == null) {
    return;
}
                

C# POST Request

var request = (HttpWebRequest)WebRequest.Create("http://pcmiler.alk.com/APIs/REST/v1.0/Service.svc/mapRoutes?dataset=Current");       
var postData = "{ MyPostQuery }";
var data =  Encoding.ASCII.GetBytes(postData);
request.Headers.Add("Authorization", apiKey.Text);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
if (response.ContentType.StartsWith("image"))
{
    Image mapImage = Image.FromStream(response.GetResponseStream());
    pictureMap.Size = mapImage.Size;
    mapImage1 = mapImage;
    pictureMap.Visible = true;
    pictureMap.Image = mapImage;
    txtResponse.Visible = false;    
}
                

Javascript POST Request

reqhttp = new XMLHttpRequest();
var url = "http://pcmiler.alk.com/APIs/REST/v1.0/Service.svc/mapRoutes?dataset=Current";
reqhttp.open("POST", url, true);
reqhttp.setRequestHeader("Content-type", "application/json");
reqhttp.setRequestHeader("Authorization", apikey);
reqhttp.responseType = "arraybuffer";

reqhttp.onreadystatechange = function () { //Call a function when the state changes.
    if (reqhttp.readyState == 4 && reqhttp.status == 200) {

        var res = reqhttp.response;
        if (res) {

            var uInt8Array = new Uint8Array(res);
            var i = uInt8Array.length;
            var binaryString = new Array(i);
            while (i--) {
                binaryString[i] = String.fromCharCode(uInt8Array[i]);
            }
            var data = binaryString.join('');

            var base64 = window.btoa(data);

            document.getElementById("myImage").src = "data:image/png;base64," + base64;
        }
    }
}

var parameters = "{ MyPostQuery}";

doFunction();
function doFunction() {
    reqhttp.send(parameters);
}