<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to get JSON data via API?]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I will collect data via API for our project. I would like to get the readValue of each device but I couldn't find the way to get it. Because I am very new at javascript and API environment.</p>
<p dir="auto">The Data API format is shown below. In order to get data, I have tried 2 different methods:</p>
<p dir="auto"><strong>Method 1:</strong><br />
I have added HTTP retriever datasource and added a point. Then I get an error.</p>
<p dir="auto">Is there any suggestion, what is the best way/datasource to get this data?<br />
How do we enter authorization info (basic, bearer etc.) to http datasources?</p>
<p dir="auto"><strong>HTTP retriever url:</strong>       <a href="http://10.35.173.145:84/api/Lin" rel="nofollow ugc">http://10.35.173.145:84/api/Lin</a><br />
<strong>Point Value RegEx:</strong>      .<em>readValue":(\d+).</em>(?=.<em>"deviceName":"PDU-24A-42F11".</em>).<br />
<strong>Error:</strong>     Invalid response from "<a href="http://10.35.173.145:84/api/Lin" rel="nofollow ugc">http://10.35.173.145:84/api/Lin</a>": 401</p>
<pre><code>//url: http://10.35.173.145:84/api/Lin
// Bearer Auth code: eyJhbGciOiJIUzI1NiIsInR5
{
    "lastUpdate": "2020-06-10T10:52:39.8352606+03:00",
    "result": [
        {
            "deviceId": 27,
            "deviceName": "PDU-24A-42F11",
            "room": "DC_KAR",
            "row": "M_KAR",
            "location": "SOL",
            "tagId": 20987,
            "tagName": "Voltage",
            "unit": "V",
            "readDate": "2020-06-10T10:51:45",
            "readValue": 228.87
        },
        {
            "deviceId": 33,
            "deviceName": "PDU-23B-7F1",
            "room": "DC_KAR",
            "row": "M_KAR",
            "location": "SOL",
            "tagId": 21155,
            "tagName": "Voltage",
            "unit": "V",
            "readDate": "2020-06-10T10:52:23",
            "readValue": 228.97
        },
        {
            "deviceId": 34,
            "deviceName": "PDU-23B-29F11",
            "room": "DC_KAR",
            "row": "M_KAR",
            "location": "SOL",
            "tagId": 21183,
            "tagName": "Voltage",
            "unit": "V",
            "readDate": "2020-06-10T10:51:31",
            "readValue": 228.97
        },
        {
            "deviceId": 35,
            "deviceName": "PDU-23B-25F11",
            "room": "DC_KAR",
            "row": "M_KAR",
            "location": "SOL",
            "tagId": 21211,
            "tagName": "Voltage",
            "unit": "V",
            "readDate": "2020-06-10T10:51:00",
            "readValue": 228.87
        },
        {
            "deviceId": 42,
            "deviceName": "PDU-23B-33F11",
            "room": "DC_KAR",
            "row": "M_KAR",
            "location": "SOL",
            "tagId": 21327,
            "tagName": "Voltage",
            "unit": "V",
            "readDate": "2020-06-10T10:51:34",
            "readValue": 228.97
        },
        {
            "deviceId": 30,
            "deviceName": "PDU-23B-38F11",
            "room": "DC_KAR",
            "row": "M_KAR",
            "location": "SOL",
            "tagId": 21071,
            "tagName": "Voltage",
            "unit": "V",
            "readDate": "2020-06-10T10:52:30",
            "readValue": 228.89999
        }
}
</code></pre>
<p dir="auto"><strong>Method 2:</strong><br />
I have added a global script shown below. When I print the content, I see the data. But I cannot get the read value of device. How can I get it?</p>
<pre><code>function linye(message) {
  
     HttpBuilder.request({
    	path: "http://10.35.173.145:84/api/Lin",
    	method: "GET",
    	headers: {
    	    ContentType : "application/json",
            Authorization : "Bearer eyJhbGciOiJIUzI1NiIsInR5",
    	},

    	err: function(status, headers, content) { //errorCallback for linguistic completion
    		throw "Request got bad response: " + status+content;
    	},
    	resp: function(status, headers, content) { //responseCallback
    	    
    		return true; //will print in wrapping print()
    	},
    	excp: function(exception) { //exceptionCallback
    		throw exception.getMessage();
    	}
    })
}
</code></pre>
<p dir="auto">Thanks in advance!</p>
]]></description><link>https://forum.mango-os.com/topic/4834/how-to-get-json-data-via-api</link><generator>RSS for Node</generator><lastBuildDate>Wed, 13 May 2026 19:02:29 GMT</lastBuildDate><atom:link href="https://forum.mango-os.com/topic/4834.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Jun 2020 10:32:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to get JSON data via API? on Tue, 16 Jun 2020 06:42:07 GMT]]></title><description><![CDATA[<p dir="auto">Excellent news, always happy to help</p>
<p dir="auto">Fox</p>
]]></description><link>https://forum.mango-os.com/post/25330</link><guid isPermaLink="true">https://forum.mango-os.com/post/25330</guid><dc:creator><![CDATA[MattFox]]></dc:creator><pubDate>Tue, 16 Jun 2020 06:42:07 GMT</pubDate></item><item><title><![CDATA[Reply to How to get JSON data via API? on Tue, 16 Jun 2020 06:00:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mattfox" aria-label="Profile: MattFox">@<bdi>MattFox</bdi></a>  Thank you for your time and solution. I did what you have said and it works !!</p>
]]></description><link>https://forum.mango-os.com/post/25329</link><guid isPermaLink="true">https://forum.mango-os.com/post/25329</guid><dc:creator><![CDATA[Malaguena]]></dc:creator><pubDate>Tue, 16 Jun 2020 06:00:50 GMT</pubDate></item><item><title><![CDATA[Reply to How to get JSON data via API? on Wed, 10 Jun 2020 20:49:12 GMT]]></title><description><![CDATA[<p dir="auto">I'll help you out Nurr, let's simplify things a bit shall we?</p>
<p dir="auto"><strong>METHOD 1</strong></p>
<ol>
<li>Complete method 2 below</li>
<li>Create a virtual datasource to host the data, make sure each point created <em>is Settable</em> and set the XIDs to be the same as the devicenames if they are all different. Need them all to be unique.</li>
<li>create a scripting datasource or a meta point datasource.</li>
<li>Use the script in method 2 to make the http request to get your data.</li>
<li>Attach to the script context point, all of the points you made in your virtual datasource.</li>
<li>In your callback use:</li>
</ol>
<pre><code>var foxesCBHandler = function(status, headers, content)
{
   var data = JSON.parse(content);
   data = data.result; //lets get the info we want.
    for (var pnt in CONTEXT_POINTS) //list of points in context
    {
        for(var i=0; i&lt;data.length; i++) //loop through available values.
        {
            if(this[pnt].xid === data[ i ].deviceName) //items match
            {
                this[pnt].set(data[ i ].readValue, Date.parse(data[ i ].readDate) ); //set value and time
                break; //finished, next context pt!
             }
        }
    }
}
</code></pre>
<ol start="7">
<li></li>
</ol>
<pre><code>path: "http://10.35.173.145:84/api/Lin",
    	method: "GET",
    	headers: {
    	    ContentType : "application/json",
            Authorization : "Bearer eyJhbGciOiJIUzI1NiIsInR5",
    	}
</code></pre>
<p dir="auto">Becomes</p>
<pre><code>httpReq("http://10.35.173.145:84/api/Lin", "get", foxesCBHandler,  {
    	    ContentType : "application/json",
            Authorization : "Bearer eyJhbGciOiJIUzI1NiIsInR5"
    	});
</code></pre>
<ol start="8">
<li>set your script to run as often as you require ie every 15 mins.</li>
</ol>
<p dir="auto"><strong>METHOD 2</strong><br />
Create a global script called HTTP REQUESTS.<br />
Inside I wrote this wrapper to handle all http related requests from all of my scripts/meta data scripts.</p>
<pre><code>function httpReq(url,method,callback,headers,params)
{//Second format, full request
    if(headers===undefined){headers={};}
    if(params===undefined){params={};}
    if(method=="get")
    {
    HttpBuilder.get(url,headers,params)
	.err(function(status, headers, content) { //setErrorCallback for linguistic completion
		  //setErrorCallback for linguistic completion
            	print(status);	print(content);
	}).resp( 
	 callback

 ).excp(function(exception) { //setExceptionCallback
		throw exception.getMessage();
	}).execute();
    }
    else
    {
         HttpBuilder.post(url,headers,params)
	.err(function(status, headers, content) { //setErrorCallback for linguistic completion
		  //setErrorCallback for linguistic completion
            	print(status);	print(content);
	}).resp( 
	 callback
	
	 ).excp(function(exception) { //setExceptionCallback
		throw exception.getMessage();
	}).execute();
    }
}
</code></pre>
<p dir="auto">This simplifies things because now all you need to do is write your callback function to handle your response:</p>
<pre><code>var handleResponse = function(status, headers, content){  /*play with content */};
</code></pre>
<p dir="auto">Then make your request inside the same script with:</p>
<pre><code>var url = "https://api.domain.com";
var method = "get";  //Or "post"
var headers = {ContentType : "application/json",};
var parameters ={}; /* If needed exclude if not*/
httpReq(url, method, handleResponse , headers);

/*httpReq(url, method, handleResponse , headers, parameters);*/

/*can just do  httpReq("https://api.domain.com" ,"get",handleResponse,{ContentType : "application/json",Authorization : "Bearer eyJhbGciOiJIUzI1NiIsInR5"}) */
</code></pre>
<p dir="auto">This makes handling your http callbacks a bit more easily and just makes your scripts that little bit tidier...</p>
<p dir="auto">Fox</p>
]]></description><link>https://forum.mango-os.com/post/25296</link><guid isPermaLink="true">https://forum.mango-os.com/post/25296</guid><dc:creator><![CDATA[MattFox]]></dc:creator><pubDate>Wed, 10 Jun 2020 20:49:12 GMT</pubDate></item></channel></rss>