Mihai Galos
Mihai Galos

Reputation: 1897

Making an Ajax request in Amazon S3 using a Lambda function

I'm trying to fetch a JSON response with weatherdata coming from the Netatmo Cloud, using a lambda function/javascript in Amazon S3 AWS. I am first trying to fetch a token using the following method. It seems that the dollar sign is not recognized. What gives?

function getNetatmoData(){
var clientId = "******";
var clientSecret = "******";

var userId="******@******.com"; 
var parola="******"; 
var formUserPass = { client_id: clientId, 
client_secret: clientSecret, 
username: userId, 
password: parola, 
scope: 'read_station', 
grant_type: 'password' };

$.ajax({
 async: false,
url: "https://api.netatmo.net/oauth2/token",
  type: "POST",
  dataType: "json",
  data: formUserPass,
  success: function(token){
        // do something awesome with the token..
    }

});


console.log("http request successful...");  

}

Upvotes: 0

Views: 1556

Answers (1)

Jaguilar
Jaguilar

Reputation: 111

Looks like your trying to use a jQuery ajax method. If jQuery isn't loaded this won't work. I'm not very familiar with the AWS lambda interface so if it is possible to load jQuery before the script is run, that would seem to be your best bet.

Your other option would be a vanilla javascript XMLHttpRequest. I peeked around at Netatmo's documentation and it looks like this should work

function getNetatmoData(){
var clientId = "******";
var clientSecret = "******";

var userId="******@******.com"; 
var parola="******"; 
var formUserPass = { client_id: clientId, 
    client_secret: clientSecret, 
    username: userId, 
    password: parola, 
    scope: 'read_station', 
    grant_type: 'password' };
var req = new XMLHttpRequest();
req.open('POST',"https://api.netatmo.net/oauth2/token", false);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

req.onload = function (e) {
    if (req.status == 200) {

        console.log('http request was successful', req.response)
    }
    else if (req.status == 400) {
        console.log('There was an error')
    }
    else {
        console.log('There was something else that went wrong')
    } 
}
req.onerror = function (e) {
    // Do something about the error
    console.log("There was an error", req.response);
}
req.send(formUserPass);
}

Upvotes: 1

Related Questions