• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing netPI web UI with REST API
#1
You all know the tile Control Panel in netPI's web GUI you click on to make a basic setup.

But what is if you want to configure those settings from remote over Ethernet instead of using mouse and keyboard? Or even better from a container running on netPI ?

All you need to know is how to access the Control Panel's HTTP RESTful API. Here is the example if you want to do it from a container:

STEP 1: Load the Raspbian container from here onto netPI.

Choosing the network mode host or bridge decides on the destination IP address you have to address later

For network mode host (container shares TCP/IP stack of host) use the "localhost" IP address 127.0.0.1 as <IP address>


For network mode bridge (container has its own TCP/IP stack, bridge by a bridge) use the "docker0" bridge default IP address 172.17.0.1 as <IP address>

STEP 2: Login to this container with a terminal over SSH as described in the container's documentation.

STEP 3: Install the http CLI command (or any other command like curl supporting http requests) in the container with

$ apt-get update && apt-get install httpie

STEP 4: Login

Call

$ http --verify=no --json POST "https://<IP address>/login" username=admin password=<your admin password>

and you get a response back like this

HTTP/1.1 302 Found

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Connection: keep-alive
Content-Length: 47
Content-Type: text/plain; charset=utf-8
Date: Wed, 05 Sep 2018 12:50:52 GMT
Location: /content/?page=infocenter
Server: nginx/1.12.1
Vary: Accept, Accept-Encoding
X-Powered-By: Express
set-cookie: sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU; Path=/; HttpOnly

Found. Redirecting to /content/?page=infocenter

The most important part of the response is the value set-cookie that has to be used for all subsequent http commands as session token.

STEP 5: Get Info Center HTML page

Call

$ http --verify=no --json GET  "https://<IP address>/content/?page=infocenter" 'Cookie:sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU'

and you get a response back like this

HTTP/1.1 200 OK

Accept-Ranges: bytes
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Cache-Control: public, max-age=0
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Wed, 05 Sep 2018 14:36:11 GMT
ETag: W/"72d-1653d845b68"
Last-Modified: Wed, 15 Aug 2018 12:16:49 GMT
Server: nginx/1.12.1
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: Express

<!-- Copyright © 2016 Hilscher Gesellschaft fuer Systemautomation mbH See Hilscher_Source_Code_License.txt -->
<!DOCTYPE HTML>
<html>
<head>
  <title>Control Panel</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta http-equiv='Content-Type' content='text/html;charset=UTF-8' />
  <script type="text/javascript" src="netIOTAccessPages/js/zxcvbn.js"></script>
  <script src="../../resources/sap-ui-core.js"
          id="sap-ui-bootstrap"
          data-sap-ui-libs="sap.ui.commons,sap.ui.table,sap.ui.unified, sap.ui.core, sap.m"
          data-sap-ui-theme="sap_goldreflection">
  </script>

  <!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
  <link rel="stylesheet" type="text/css" href="css/animate.css">
  <link rel="stylesheet" type="text/css" href="css/WebCommissioning.css">
  <link rel="stylesheet" type="text/css" href="css/OpenUI5Hilscher.css">
  <script src="../../socket.io/socket.io.js"></script>
  <script type="text/javascript" src="lib/jquery.noty.packaged.min.js"></script>
  <!--<script src="lib/logger.min.js"></script>-->
  <script type="text/javascript" src="DataModel/dataModels.js"></script>
  <script type="text/javascript" src="Structures/CommissioningStructs.js"></script>
  <script type="text/javascript" src="Helper/PasswordHelper.js"></script>
  <script type="text/javascript" src="Helper/DataTypeHelper.js"></script>
  <script type="text/javascript" src="Helper/DataValidationHelper.js"></script>
  <script type="text/javascript" src="Helper/IPTextFieldHelper.js"></script>
  <script type="text/javascript" src="WebCommissioning.js"></script>
</head>
<body class="sapUiBody" role="application">
  <div id="content"></div>
  <!--<div id="container"></div>
  <div id="log_messages"></div>-->
</body>
</html>

STEP 6: LAN network settings

Call

$ http --verify=no --json POST "https://<IP address>/set/networkSettings/LANSettings" 'Cookie:sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU'  'networksettings=[{"name":"eth0","macaddress":"b8:27:eb:23:7a:c1","dhcp":true,"ipaddress":["10","11","5","18"],"networkmask":["255","255","0","0"],"gateway":["10","11","0","10"],"dns":[["192","168","100","21"],["192","168","100","22"],"192.168.100.50"]}]' 'category=LAN'

and you get a response back like this

HTTP/1.1 200 OK

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Connection: keep-alive
Content-Length: 15
Content-Type: application/json; charset=utf-8
Date: Wed, 05 Sep 2018 14:31:04 GMT
ETag: W/"f-v/Y1JusChTxrQUzPtNAKycooOTA"
Server: nginx/1.12.1
Vary: Accept-Encoding
X-Powered-By: Express

{
    "status": "OK"
}

Today we have no full documentation of the RESTful API ready for you.

Instead I recommend to use web browsers like Firefox or Google Chrome to analyse the RESTful API using "development sessions" like the appended picture shows.

Both browsers can record with their embedded network traffic analyser the HTTP requests they are sending during your manual mouse click/keyboard sessions you can analyse afterwards in terms of web page, http header, cookie, parameters .... Then you can construct exactly the same requests with the http CLI command (or any other HTTP capable program) as shown above.


Attached Files Thumbnail(s)
   
You never fail until you stop trying.“, Albert Einstein (1879 - 1955)

  Reply
#2
Find netPI's official RESTful API documentation in writing (YAML format) appended to this post.

To read the documentation open the online editor Swagger in your browser. Swagger is an open source project and delivers a set of tools to document APIs.

In the editor choose "File -> Import File" to import the appended file or copy and paste its contents to the editor's left window manually.

The editor works interactive and is made for reading and writing APIs both. As soon as the left windows contains YAML formatted information you can read/click the output in the right window.


Attached Files
.txt   RESTful_api_1.0.0.txt (Size: 77.92 KB / Downloads: 45)
You never fail until you stop trying.“, Albert Einstein (1879 - 1955)

  Reply
#3
Dear Armin,

In the RESTful_api.1.0.0.txt, which is the API used to update the Docker container?
If possible, I'd like to have an example of access to REST API for updating Docker container, please.

Also, are the following web site valid for netPI?
https://www.netiot.com/external/stkit-doku/

Thank you for your information.
Best regards,
  Reply
#4
Hello Tad,

this API reference is the wrong API for you because this it the API of netPIs UI. What you need is the Docker API supported by the separated Docker icon and service provided by portainer.io which is described here https://forum.hilscher.com/thread-369.html.

In this thread you find also a wonderful Node-RED flow as example which you can use to access your netPI from an external PC for example where you have a running Node-RED. The first thing you have to do after you copied the flow is to go use the injections nodes stepwise. So start with STEP 1 using the node-red function node "OPEN ME ... to set your credentials" and enter all you relevant parameters of your netPI like IP address, username, password, your docker repo "hilschernetpi/netpi-raspbian" for example, your tag like "latest", docker username and password (optional) and the registry you want to pull from like the official Docker Hub registry "registry.hub.docker.com". This node-red function node stores all this in a global variable.

After that you can use all the other injection nodes STEP X wise.

Thx
Armin
You never fail until you stop trying.“, Albert Einstein (1879 - 1955)

  Reply
#5
Dear Armin,


Thank you very much for your quick response.

You means the APIs for the tile 'Control Panel' is described here, but the APIs for the tile 'Docker', which launches portainer.io, is described at Thread 369?
(If I want to update a Docker container, do I need to refer to the thread 369?)


In this case, I think I should set up Node-RED in my Windows PC or I need second netPI, to run/evaluate the sample flow shown in the thread 369.
Is that right?

Thank you for confirming my understandings.
Best regards,
  Reply
#6
Well Tad,

the netPI main Web-UI, control panel etc. was developed 100% by Hilscher. So it has a separate API as described in this current thread-317. So you could setup the time, network configuration, enable/disable services like Docker and so on.

But the Docker functionality was 100% not developed by Hilscher. We have just added Docker and portainer.io to the netPI OS and put the nginx reverse proxy to its front. This means in turn that also the very official API of portainer.io was also implemented implicitly and this is why controlling Docker is in accordance to thread-369.

Since Node-RED can run under Windows https://nodered.org/docs/getting-started/windows this is an easy evaluation method for anyone who has an PC to check the API. I agree of course since Node-RED can run on any machine that also a second netPI with installed Node-RED could be used. But this needs a second hardware which might be a problem for some customers.

The thread-369 uses Node-RED as example which is for a non-programmer better to understand. Of course a real Linux programmer would never use Node-RED to access the API but more a C-coded program or Linux commands like "curl" to access the REST-Api. But Node-RED is a good starter. With netPI we use portainer 1.20.1 and its very official API documentation is located here https://app.swaggerhub.com/apis/devianto...ner/1.20.1. So you see this is not Hilscher specific but 100% portainer.io.

Thx
Armin
You never fail until you stop trying.“, Albert Einstein (1879 - 1955)

  Reply
#7
Dear Armin,

I can shut down netPI by executing the following 2 commands.
I am now planning to execute them from a Python program which is triggered from Node-RED dashboard.

http --verify=no --session=logged-in --json POST "https://<IP address>/login"  username=<username> password=<password>
http --verify=no --session=logged-in --json POST "https://<IP address>/system" argument=shutdown

But, the following response will come.
'System will shutdown after 10 seconds'

Just in case, I would like to ask you that: is it possible to shutdown netPI immediately without 10 seconds delay?

Thank you very much for your information.
Best regards,
  Reply
#8
Hello Tad,

I am sorry to say that this cannot be changed since it is statically implemented in the Linux host system and executed shutdown script. You have to access to it to change it.

Thx
Armin
You never fail until you stop trying.“, Albert Einstein (1879 - 1955)

  Reply
#9
In this thread you find another method to execute a forced shutdown in a containter

https://forum.hilscher.com/thread-611-page-2.html
You never fail until you stop trying.“, Albert Einstein (1879 - 1955)

  Reply
#10
Dear Armin,

I am now trying to create an html file on a Windows 10 PC to set up the contents of Control panel.
When a function 'XMLHttpRequest' is called from the html file to communicate with netPI using POST & GET, the attached CORS error appears.

I couldn't solve the error by:
- A Chrome extension called Access Control-Allow-Origin -Unblock (https://chrome.google.com/webstore/detai...no/related) was installed not to be requests blocked
- The html file was executed on Node-RED on a Windows 10 PC

Do you have any solutions to execute REST API from an html file on a Windows 10 PC?
I want to do 'setup' & 'backup' all data to/from netPI's Control panel for mass-production.

By the way, as far as I investigated, I suppose the error was caused by server (netPI) side.
Because I think netPI doesn't include 'Access-Control-Allow-Origin:' in its header area.
If a next netPI firmware version adapts the above, that would be helpful for me.

Thank you very much for your advice.
Best regards,


Attached Files Thumbnail(s)
   
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  REST API server data communication MGharat 2 113 July-21st-2021, 07:01 AM
Last Post: MGharat
  Get host MAC via REST API inside container bschandra 4 1,248 November-16th-2020, 09:26 AM
Last Post: bschandra
  Accessing a modbus device connected to RTE port from Node-RED tad 10 2,450 October-2nd-2020, 07:21 AM
Last Post: Armin@netPI
  Web browser reports "insecure connection" when accessing netPI for the first time Armin@netPI 2 1,150 January-31st-2020, 07:07 AM
Last Post: Armin@netPI
Information netPI Docker REST API patrick 1 1,474 July-16th-2019, 02:45 PM
Last Post: patrick
  Accessing Docker daemon through remote CLI adeeljsid 2 1,383 August-30th-2018, 12:32 PM
Last Post: Armin@netPI

Forum Jump:


Users browsing this thread: 1 Guest(s)