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.
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. |
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();
Java POST Request
String address = ("https://pcmiler.alk.com/APIs/REST/v1.0/Service.svc/route/routeReports?dataVersion=Current"); URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", API_KEY); String postData = "{ \"ReportRoutes\": [{\"RouteId\": \"test\", \"Stops\": [{\"Address\": {\"Zip\": \"08540\"},\"Label\": \"Trimble MAPS\"}, {\"Address\": {\"Zip\": \"90266\"},\"Label\": \"Manhattan Beach\"}], \"ReportTypes\": [{\"__type\": \"MileageReportType:http://pcmiler.alk.com/APIs/v1.0\",\"THoursWithSeconds\": false}]}]}"; conn.setRequestProperty("Content-Length", Integer.toString(postData.length())); OutputStream os = conn.getOutputStream(); os.write(postData.getBytes()); os.flush(); if (conn.getResponseCode() != 200) if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) { throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode()); } BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString());
C# POST Request
var request = (HttpWebRequest)WebRequest.Create("https://pcmiler.alk.com/APIs/REST/v1.0/Service.svc/route/routeReports?dataVersion=Current"); var postData = "{ \"ReportRoutes\": [{\"RouteId\": \"test\", \"Stops\": [{\"Address\": {\"Zip\": \"08540\"},\"Label\": \"Trimble MAPS\"}, {\"Address\": {\"Zip\": \"90266\"},\"Label\": \"Manhattan Beach\"}], \"ReportTypes\": [{\"__type\": \"MileageReportType:http://pcmiler.alk.com/APIs/v1.0\",\"THoursWithSeconds\": false}]}]}"; var data = Encoding.ASCII.GetBytes(postData); request.Headers.Add("Authorization", API_KEY); 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(); var responseJSON = new StreamReader(response.GetResponseStream()).ReadToEnd(); Console.WriteLine(responseJSON);
Javascript POST Request
reqhttp = new XMLHttpRequest(); var url = "https://pcmiler.alk.com/APIs/REST/v1.0/Service.svc/route/routeReports?dataVersion=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(''); document.getElementById("textbox").innerText = data; } } } var postData = "{ \"ReportRoutes\": [{\"RouteId\": \"test\", \"Stops\": [{\"Address\": {\"Zip\": \"08540\"},\"Label\": \"Trimble MAPS\"}, {\"Address\": {\"Zip\": \"90266\"},\"Label\": \"Manhattan Beach\"}], \"ReportTypes\": [{\"__type\": \"MileageReportType:http://pcmiler.alk.com/APIs/v1.0\",\"THoursWithSeconds\": false}]}]}"; doFunction(); function doFunction() { reqhttp.send(postData); }