Restful API with Expressjs/Angularjs

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: https://github.com/request/request#readme
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.

Update: if I try to send the request through Postman this is what happens.

Welcome to the Forums!

Seems like a networking issue.
Please take a screen shot and post it to this thread of the PAC Manage status read page for the networking details on this PAC Controller.

1 Like

Thanks for helping. Is this what you asked for?.

I don’t understand this issues, cause it does work if I manually insert the URL and credentials google chrome browser

1 Like

Yep, thats exactly what I was after. Thanks for posting it.

The network settings look good.
We (I have our developer @torchard working on your post as well) are trying to figure why its working from your computer - at first we thought it was a networking issue not having the gateway set, or the subnet mask mixed up, but it all looks good… so we will consider what else might be an issue.

1 Like

I’m having a lot of trouble duplicating this on my end; I’m able to see the data come up in the browser just as you described, but when I test it through Postman everything works as expected.

Do you have your “Content-Type” header set to “application/json”?

Here’s what that looks like for me in my Postman results:

image

If you’re not seeing that, you could try setting that header specifically for the request:

Oh god I feel embarrassed now… All I had was a spelling issue with the URL. My controller URL is 192.168.0.3, the one I introduced in the browser was correct but the one I introduced on my expressjs app and on postman was 198.186.0.3. Once I fixed that it worked in both places and I am successfully getting the response.

Thank you so much for spending time helping me out. To compensate I will upload later my web app, and maybe it could be part of the list of Pac control REST API code samples for snap pac.

Thank you Beno and Torchard.

2 Likes

Glad you are now up and running.
Thanks for letting us know.