I am having trouble with the Web Dev module, I am using the Python resource. In the drop down I have selected the doPatch. I cant find any examples of what my code should look like. Where do I place the code? I do not have a lot of experience and I rely heavily on sample code or example code to accomplish things. Any help is much appreciated.
Letâs back up a bit. Whatâs the goal youâre trying to use Webdev to accomplish?
The webdev module allows you to expose Ignitionâs built-in webserver for some amount of custom behavior - Python resources are the standout example, but to make use of them youâll want to know some things.
First, HTTP verbs. REST is one âstandardâ for what HTTP verbs should do, and often useful as a guideline. In a REST model, generally speaking,
GET returns details of a resource
POST creates a new resource
DELETE removes a resource
PATCH updates an existing resource
So if you havenât already implemented the rest of a REST API inside Webdev, thereâs no special need to use PATCH. If you want to render a page that is directly usable inside a web browser, then youâll want to start with GET/doGet. In the simplest form - when a user navigates to ${yourGatewayAddress}/system/webdev/${yourProject}/${path/to/your/resource}, then Ignition will run your defined handler for whatever HTTP verb the request was made using (GET being the default unless otherwise specified), and return the results to the user. If you enable the default placeholder doGet method, youâll get a very basic âhello worldâ style HTML return - HTML is the markup language browsers most commonly use.
Thank you for that clarification. I think I misunderstood what the Web Dev module does when we purchased it. My goal was to send information from Ignition to an outside API. If I am understanding you correctly the Web Dev would allow me to expose Ignition as an API for a 3rd party to retrieve information. Or build a webpage with information and other web development? This area is new to me and I am really appreciative of the information.
On another note it appears that I can send information to an API just using scripting. I installed a couple different python packages, ârequestsâ and âunirestâ, and I have had success sending information out to the API and getting a response using both. So as of right now I may not need to utilize the Web Dev module. I am sure that I will have a need for it in the future though. Once again thank you for the clarification. Now I have a pretty clear picture of where I need to go from here.
The web dev module is more for setting up your own API that others could interact with if my understanding is correct. That way someone could do a GET request to your Ignition server and you could give back some data based on the nature of their request.
WebDev offers REST APIâs for getting information/resources from Ignition (such as HTML pages or Tag values etc) into a browser using GET / POST etc HTTP methods. To get an idea what sort of things you can build WebDev please see our module on EXCHANGE portal:
Yes, exactly. WebDev exposes Ignition to other consumers, the system.net functions allow you to broadcast to other web APIs. Thereâs also Sepasoftâs Web Services module available, for a somewhat more âbatteries-includedâ approach, though it has certain requirements for services, if I remember correctly.
Thank you for pointing me in that direction. Do you happen to know how I need to use authentication in this script with a username and password? I looked at the Basic authentication that it supports but unfortunately I do not understand it.
Start your script by creating the httpClient object with a username and password. Then you can use that client object with multiple operations, and all will have the basic authentication headers inserted automatically. Something like this:
Basic authentication is one (somewhat outdated) style of authentication. Most external services won't actually support it. Commonly, web APIs will allow users to generate some kind of long-lived token, and then require that token be passed in an authentication header - with httpClient, that would look something like this:
So this I understand this example pretty well and can adapt to my needs. But I got a lot of errors when using it. I have had success with the âunirest.getâ as far as getting information from the API. But when I use this to get information using the same URL and same credentials it doesnât work. Other than the error ânot able to getâ I am seeing this âWWW-Authenticate header missing for response code 401â. I am not sure what that means.
I am going to try really hard to use this. But it may be outside my skillset at this time. I was only provided credentials and the URL with parameters to read/write. I am going to have to do some research and learning to get to understand what is going on here. I have used API Keys before dealing with Node-Red but this is new to me. I am always up for a challenge
Typically you have to ask the API for the token, it will be some long string of characters. You normally have to go to the development/developers part of the service to be able to ask them to generate you a token.
401 means you had an issue with your authorization. The error message says your missing a header, which unirest may be adding for you behind the scenes.
Code 401 means the web service is denying access, but the missing header means it is declining to indicate what mechanism to use. If it had provided this header with its response, and it specified "Basic", then the httpClient() would have been able to handle it for you.
You will need help from the service provider to know what header to supply, with suitable content. Besides Paul's suggestion, it could also require a cookie, for which you would use the httpClient's cookie manager.
I finally got this to work. For some reason I had to move the authentication into the requests I was making. I don't know why that made a difference though?
EDIT-
I called tech support and it was explained to me that the difference is one authentication is in the header (your code) and the other is in the body(my code). It just depends on where the API is looking for it? From my understanding its most of the time in the header though.
A good resource if you are just starting to work with APIs would be Postman. This allows you to create the API calls and see down the application stack where they all fit together and what a finalized request might look like. https://www.postman.com/