# RESTful API instruction of ePaper Manager ###### tags: RESTful API ## How To Use RESTful API **Example API: <font style="background-color: yellow;">Get Tay By Agent id</font>** #### If you want to retrieve EPD tag information by a specific agent id, you can use this API. ![](https://i.imgur.com/kHejBoA.png) ![](https://i.imgur.com/nXBFVwJ.png) ### Javascript **Prerequisite:** Include jQuery CDN in the HTML head element ``` <script src="https://code.jquery.com/jquery.js"></script> ``` ```javascript // Custom parameters var agentid = "00000001-0000-0000-0012-4b001ae6ec05"; var url = "http://172.22.13.3:8080/esl/v1/tags/tag/?agentid=" + agentid; // URL-Path var type = "GET"; // Method var data = {}; // Request-body var username = "root"; // ePaper Manager Username var password = "P@ssw0rd"; // ePaper Manager Password var auth = "Basic " + btoa(username + ":" + password); // Authorization // Query API api(url, type, data, auth, function(res){ console.log(res); }); // We use AJAX method to make a query function api(url, type, data, auth, callback){ $.ajax({ url: url, type: type, data: JSON.stringify(data), dataType: "json", headers: { "Authorization": auth, }, xhrFields: { withCredentials: true }, contentType : 'application/json', // Content-Type: application/json error: function(xhr) { console.log("Ajax request error!"); // Response-body (Error) }, success: function(res) { callback(res); // Response-body (Success) } }); } ``` ### Java ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; import org.json.JSONObject; public class Test { public static void main(String[] args) { try { // Custom parameters String agentid = "00000001-0000-0000-0012-4b001ae6ec05"; String url = "http://172.22.13.3:8080/esl/v1/tags/tag/?agentid=" + agentid; // URL-Path String type = "GET"; // Method String username = "root"; // ePaper Manager Username String password = "P@ssw0rd"; // ePaper Manager Password String auth = "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes("UTF-8")); JSONObject data = new JSONObject(); // Request-body // Query API JSONObject result = api(url, type, auth, data); System.out.println(result); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } private static JSONObject api(String url, String type, String auth, JSONObject data) { URL uri; HttpURLConnection conn = null; JSONObject result = new JSONObject(); BufferedReader br = null; try { uri = new URL(url); conn = (HttpURLConnection) uri.openConnection(); conn.setRequestMethod(type); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setRequestProperty("Content-Type", "application/json"); // Content-Type: application/json conn.setRequestProperty("Authorization", auth); conn.setDoOutput(true); String reqBody = data.toString(); if (!"{}".equals(reqBody)) { conn.setDoInput(true); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.write(reqBody.getBytes("utf-8")); out.flush(); out.close(); } conn.connect(); System.out.println("StatusCode: " + conn.getResponseCode()); br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String read = new String(); while ((read = br.readLine()) != null) { result = new JSONObject(read); // Response-body } } catch (Exception e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } if (conn != null) conn.disconnect(); } return result; } } ``` ### C# ```C# using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.IO; using Newtonsoft.Json.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { // Custom parameters string agentid = "00000001-0000-0000-0012-4b001ae6ec05"; string url = "http://172.22.13.3:8080/esl/v1/tags/tag/?agentid=" + agentid; // URL-Path string type = "GET"; // Method string username = "root"; // ePaper Manager Username string password = "P@ssw0rd"; // ePaper Manager Passw0rd string privilege = string.Format("{0}:{1}", username, password); byte[] bytes = System.Text.Encoding.GetEncoding("utf-8").GetBytes(privilege); string auth = "Basic " + Convert.ToBase64String(bytes); // Authorization JObject data = new JObject(); // Request-body // Query API JObject result = api(url, type, auth, data); Console.WriteLine(result); Console.ReadKey(); } static JObject api(string url, string type, string auth, JObject data) { JObject jsonObject = null; HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; req.Headers.Add("Authorization", auth); req.ContentType = "application/json"; // Content-Type: application/json req.Method = type; if (data.Properties().Count() > 0) { StringBuilder paramz = new StringBuilder(); paramz.Append(data); byte[] formData = UTF8Encoding.UTF8.GetBytes(paramz.ToString()); req.ContentLength = formData.Length; using (Stream post = req.GetRequestStream()) { post.Write(formData, 0, formData.Length); } } // Response: HttpWebResponse resp = null; try { resp = (HttpWebResponse)req.GetResponse(); } catch (WebException ex) { resp = (HttpWebResponse)ex.Response; } if (resp != null) { switch (resp.StatusCode) { case HttpStatusCode.OK: StreamReader reader = new StreamReader(resp.GetResponseStream()); string result = reader.ReadToEnd(); // Response-body (Success) jsonObject = JObject.Parse(result); break; default: Console.WriteLine("[Error] " + resp.StatusCode + ": " + resp.StatusDescription); break; } resp.Close(); } return jsonObject; } } } ``` ### Python ```python import urllib.request import urllib.parse import base64 import json def api(url, type, auth, data): req = urllib.request.Request(url) req.add_header('Authorization', auth) req.add_header('Content-Type', 'application/json') # Content-Type: application/json if len(data) > 0: jsondata = json.dumps(data) # json to string jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes req.add_header('Content-Length', len(jsondataasbytes)) res = urllib.request.urlopen(req, jsondataasbytes) else: res = urllib.request.urlopen(req) result = res.read() # Response-body jsonObject = json.loads(result) # string to json return jsonObject # Custom parameters username = 'root' # ePaper Manager Username password = 'P@ssw0rd' # ePaper Manager Password agentid = '00000001-0000-0000-0012-4b001ae6ec05' url = 'http://172.22.13.3:8080/esl/v1/tags/tag/?agentid=' + agentid # URL-Path type = 'POST' # Method base64string = str(base64.encodebytes(('%s:%s' % (username, password)).encode()))[2: -3] auth = 'Basic %s' % base64string # Authorization data = {} # Request-body # Query API result = api(url, type, auth, data) print(result) ```