2

I've got an arduino uploading sensor data to cosm.com. I made a simple webpage on my local web server to query the cosm.com API and print out the values.

The problem is that if I am not logged into cosm.com in another tab, I get this popup.

enter image description here

The solution is to pass my public key to cosm.com, but I am in way over my head here.

The documentation gives an example of how to do it in curl, but not javascript

curl --request GET --header "X-ApiKey: -Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g" https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading


How do I pass my key into the url?:

function getJson() {
$.ajax({
    type:'GET',
    url:"https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading",

//This line isn't working
    data:"X-ApiKey: -Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g",

    success:function(feed) {

     var currentSensorValue = feed.current_value;
      $('#rawData').html( currentSensorValue );
    },
    dataType:'jsonp'
});
}


UPDATE: It must be possible because hurl.it is able to query the api http://www.hurl.it/hurls/75502ac851ebc7e195aa26c62718f58fecc4a341/47ad3b36639001c3a663e716ccdf3840352645f1

UPDATE 2: While I never did get this working, I did find a work around. Cosm has their own javascript library that does what I am looking for.

http://cosm.github.com/cosm-js/ http://jsfiddle.net/spuder/nvxQ2/5/

lebreeze
  • 5,094
  • 26
  • 34
spuder
  • 17,437
  • 19
  • 87
  • 153

2 Answers2

5

You need to send it as a header, not as a query string, so try this:

function getJson() {
  $.ajax({
    type:'GET',
    url:"https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading",
    headers:{"X-ApiKey": "-Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g"},
    success:function(feed) {
     var currentSensorValue = feed.current_value;
      $('#rawData').html( currentSensorValue );
    },
    dataType:'jsonp'
  });
}
thejh
  • 44,854
  • 16
  • 96
  • 107
  • Thanks this looks really close. I still don't quite have it working. http://jsfiddle.net/spuder/Bsg7E/10/ – spuder Mar 31 '13 at 18:40
  • @spuder Maybe the API does not send the headers that are necessary to allow you to send custom headers. – thejh Mar 31 '13 at 19:17
  • While this didn't work in my case, This did explain the principle. I am marking as answered. – spuder Mar 31 '13 at 19:45
0

It should be much easier to get it to work using CosmJS. It is an officially supported library and provides full coverage of Cosm API.

errordeveloper
  • 6,716
  • 6
  • 41
  • 54