• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing netPI web GUI with HTTP REST
#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: 17)
You never fail until you stop trying.“, Albert Einstein (1879 - 1955)

  Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Accessing Docker daemon through remote CLI adeeljsid 2 161 August-30th-2018, 12:32 PM
Last Post: Armin@netPI
  Web browser reports "insecure connection" when accessing netPI for the first time Armin@netPI 0 82 December-3rd-2017, 01:40 PM
Last Post: Armin@netPI

Forum Jump:


Users browsing this thread: 1 Guest(s)