Hello,
I’ve been using Node-Red for a while but I would like to build a web app that talks to my snap-pac-R1 controller without using Node-Red. I read the Restful Api documentation and went through all the steps till this point:
I got admin and creds configurations:
I tested manually introducing an URL and providing the username and password that was asked by the browser and got my response as expected.
URL: http://192.168.0.3/api/v1/device
Response:
{"controllerType":"SNAP-PAC-R1","firmwareVersion":"R9.5a","firmwareDate":"07/07/2016","firmwareTime":"12:55:51","mac1":"00-A0-3D-03-20-4D","mac2":"00-A0-3D-03-20-4E","upTimeSeconds":4459}
Manually it works, but when I try to access the Api using Expressjs or Angularjs I’m getting “ETIMEDOUT” errors.
Server Side
On Expressjs I am using the npm request module
var express = require('express');
var router = express.Router();
const request = require('request');
//test
router.get('/test2', (req, res) => {
request.get('http://198.168.0.3/api/v1/device', function (error, response, body) {
console.error('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
res.json({body});
}).auth('Proyectos', '9861', false);
});
Got this function from the documentation: GitHub - request/request: 🏊🏾 Simplified HTTP request client.
which specifies how to make the get request and how to send the basic HTML credentials.
The response I am getting on the console is:
error: { Error: connect ETIMEDOUT 198.168.0.3:80
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '198.168.0.3',
port: 80 }
statusCode: undefined
body: undefined
Client Side
On angular I tried a different approach using the $http.get method.
var auth = $base64.encode('mainBrain:27246');
var headers = {"Authorization": "Basic " + auth};
$http({
method: 'GET',
url: 'http://198.168.0.3/api/v1/device',
headers: headers
}).then(function(data, status){
console.log("then function instantiated");
if(data){
console.log(data);
} else {
console.log("no data found");
}
},function(error){
console.log(error);
});
And also I am getting the same response on the browser’s console than on server side:
OPTIONS http://198.168.0.3/api/v1/device net::ERR_CONNECTION_TIMED_OUT
I know my issue is kind of long to read, but thanks a lot for your help. Greetings from Dominican Republic.