HTTP GET request from javascript


#1

Hi,

I wrote the following javascript code to send a HTTP request, to get data from a dataport.

var xhr = new XMLHttpRequest();
xhr.open(‘GET’, “/api/portals/v1/portals//alias?”, true);
xhr.send(none);

This doesn’t work. I tried adding authentication and still didn’t work.
xhr.setRequestHeader(“Authorization”, “Basic " + btoa(”" + “:” + “”));

I found the following link, which explains how to add GET request to a widget in the portals page. But I need the request to work in my website.

Here is a basic sketch:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8">
<meta lang=en>
<script>
    function myFunction() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', "/api/portals/v1/portals/**<insert CIK here>**/alias?**<insert alias here>**", true);
        console.log(xhr.send(null));
    }
</script>
<link rel="stylesheet" href="_styles/styles.css">
</head>  
<body>  
    <button onclick="myFunction()">Click me</button>
</body>
Can you help me fix this? @Martin @MikeA @willcharlton

Kind regards,
Vijay.


#2

@Vijay -

Is this code meant to be run in your own application or within a widget on your Portals page?

Either way you cannot use a CIK for authentication or a relative path to the API. Check out the documentation for the Portals API to fetch a dataport’s data

http://docs.exosite.com/portals/portalsapi/#get-data-source-data

curl 'https://mydomain.exosite.com/api/portals/v1/data-sources/<device_rid>/data' \
     -X GET \
     -u 'useremail@gmail.com:userP4ssword' \
     -i

This is likely the call that you will want to use to fetch your data.

Happy to help,
-Martin


#3

Hi Martin,

This is a standalone web application outside of Portals. It is a web app which will display live data trending based on data from one of my device’s data ports. I did see this Portals curl API. How do I integrate this into my javascript code of my web application?

Are there any samples?

Kind regards,
Vijay.


#4

Vijay,

I made the mistake of only now remembering that neither the RPC API or the Portals API support CORS. This means that you will not be able to use the visiting client’s browser of your site to make these requests.

A worker or process in your application will have to be responsible for fetching data via an API and then changing the client’s document.

This also has the tradeoff of allowing you to make use of the RPC API. We know that the RPC API will be more performant.

I don’t have open source code of an application that has implemented this, but I have personally helped work on them and can answer questions. There applications typically store a the subdomain’s root CIK as an environment variable to authenticate all of the RPC API requests the application needs to make.

-Martin


#5

Are you talking about this?


#6

That repository is a set of Node bindings around our RPC API, it could be of a lot of use in your JavaScript based application. Feel free to download that code and use it!

This is likely the documentation that you will want to consume: http://docs.exosite.com/portals/rpc/


#7

Hi Martin,

Thanks for your reply.

Kind regards,
Vijay.