• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing netPI web UI with REST API
#11
Well Tad,

you agree that a standard web browser you are using to access the netPI via netPI's web pages works well, or?

This means in turn that the standard web browser get managed to call the netPI RESTapi successfully and well, right? So if the web browser get managed through it ... then you should be able to execute the very same access from any other external code that is calling the RESTApi. This is my personal impression.

Since I don't know your HTML application and what parameters are really called during your external HTML access, I recommend the following first to make sure all the parameters needed for a successful API access are well know: use a standard web browser like Chrome, Firefox, Internet Explorer, doesn't matter which one, and enable the socalled "developer mode" or "debug mode" or however it is called in the web browser. In this special mode the web browser records all the accesses to web pages and files them to a long log file. This data you can watch and analyse afterwards. So if you call the netPI's login page and have the debug mode enabled and you login then ... the POST messages are recorded and you can watch them afterwards. This gives you the possibility to analyse well which cookies, parameters, codes are sent by the browser in the POSTs and compare them if they are all present in your HTML code of your Node-RED application.

So once again ... if a web browser gets managed to access the netPI, or your Linux "http ..." command line via the RESTApi then a HTML code must be able to do the same.

If of course the HTML code executer in Node-RED has a problem then I can't do much. How is your HTML code in Node-RED by the way? Can you post it?

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

  Reply
#12
Dear Armin,

>you agree that a standard web browser you are using to access the netPI via netPI's web pages works well, or?

I expect the above.
I'm not an expert for html & JavaScript so that I am not 100% sure my code is correct.

Attachments:
- index_xml.html & app_xml.js: this is a sample code for rest api by function '[font=Consolas, "Courier New", monospace]XMLHttpRequest[/font]'.
- index_fetch.html & app_fetch.js: this is a sample code for rest api by function '[font=Consolas, "Courier New", monospace]fetch[/font]'.
- nodered_flows_test_purpose.json: this is a Node-RED flow

I found the following description in a Japanese local site:
"I think that recent Chrome does not allow access to local files through httpd"
Therefore, the REST API is called on Node-RED just as a trial.

All of the above codes output the following errors to 'Console' tab of DevTools.
- Access to fetch at 'https://netpi1/login' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
- POST https://192.168.3.56/login net::ERR_FAILED
- Uncaught (in promise) TypeError: Failed to fetch


Have you ever call REST API to netPI from an html?
As far as I searched in Internet contents, the server side must include 'Access-Control-Allow-Credentials: true'[font=SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace] to be accessed from a client.[/font]
Because the above error seems to output by this reason???

Thank you very much for your support.
Best regards,


Attached Files
.zip   restapi.zip (Size: 2.73 KB / Downloads: 1)
  Reply
#13
Hello Tad,

I watched your Node-RED flow first. I cannot agree using the "http in" node and "http out" node is the correct node to use.

You know that netPI has implemented another RESTapi for managing the Docker containers next to the RESTapi for managing netPI. For this Docker API I have created a Node-RED example flow here https://forum.hilscher.com/thread-309.html. In this flow I am using the "http request" node to access the RESTapi and not "http in" and "http out" node.

So please take a chance and import this example flow, remove the parts not relevant for you and just reduce it to the login function for first steps ... and then take this simple flow as example to continue.

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

  Reply
#14
Dear Armin,

I created the attached Node-RED flow for Control Panel by referring to the Node-RED flow of the REST API for Portainer as you explained.
I was able to access netPI from Node-RED on a PC.
Thank you very much for your advice.

So, I will only continue to create Node-RED flows to access Control Panel hereafter.
(I will forget an html file anymore.)

And, I have another questions, please.
As far as I study the API for Control Panel using https://editor.swagger.io/, some APIs below mention 'category is used for user access'.
- /get/syslog/files
- /get/syslog/file
- /get/pki/files

Would you please let me know what to enter in 'category'?
I tried to enter '1' or ''(null), netPI responded to the payload as follows:
{ status: "Failed", message: "Permission denied" }

One more question is: there is a menu to enable Docker in Services-> service list.
A user can control 'operating status' and 'autostart' from its Web GUI.
Is it possible to execute them using REST API?

Thank you very much for your support.
Best regards,


Attached Files
.txt   flows_2021.06.30.txt (Size: 11.52 KB / Downloads: 0)
  Reply
#15
Hello Tad,

ALL different menus with ALL the different services you see in your web browser and all the different settings you are able to change as a human by clicking/checking/updating them with your mouse can be changed in an automated way.

What you must know is that the web browser uses exaclty the same RESTApi to control the settings as you can do it via a remote program like Node-RED. Effectively there is no difference in the view of netPI who is calling it. This is why I can say ... if you as a human in the web browser are able to change a setting manually ... then you can change the same setting with a remote program.

Now comes the best part ... if you are uncertain how exactly the RESTApi is exactly working, like the log file thing ... then use the web browser record function to record the web browser's RESTApi calls while you are clicking your mouse. It records all GET and POST etc. https RESTApi calls for you and then you can watch and analyse them afterwards and see exactly the URL called, the parameters in the request and in the response of the messages. The only thing you have to do then is to move those parameters to your specific Node-RED nodes and you will get the very same result. For this recording function you have to enable the debug/developer function in your web browser.

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

  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  REST API server data communication MGharat 2 534 July-21st-2021, 07:01 AM
Last Post: MGharat
  Get host MAC via REST API inside container bschandra 4 1,478 November-16th-2020, 09:26 AM
Last Post: bschandra
  Accessing a modbus device connected to RTE port from Node-RED tad 10 2,979 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,361 January-31st-2020, 07:07 AM
Last Post: Armin@netPI
Information netPI Docker REST API patrick 1 1,706 July-16th-2019, 02:45 PM
Last Post: patrick
  Accessing Docker daemon through remote CLI adeeljsid 2 1,667 August-30th-2018, 12:32 PM
Last Post: Armin@netPI

Forum Jump:


Users browsing this thread: 1 Guest(s)