Web API Scripting Help

Hello,

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.

3 Likes

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.

2 Likes

You can also use the built in https://docs.inductiveautomation.com/display/DOC80/system.net module to send inofmration out to an API, specifically system.net.httpPost.

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.

1 Like

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:

1 Like

Avoid using httpPost for any new applications. The much more capable httpClient is the currently recommendedd approach.

4 Likes

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.

1 Like

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:

client = system.net.httpClient(username="myuser", password="mypasswd")
....
someResponse = client.get(url="https://some.server.example.com/path/to/readable/api")
....
anotherResponse = client.patch(url="https://some.server.example.com/path/to/updateable/api", data={"arg1": "something", "arg2": "something else"})
2 Likes

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:

client = system.net.httpClient()

headers = {
	"Authorization": "Bearer %s" % token
}

response = client.post("url", headers=headers)

Or for a more complete example of wrapping a web API:

3 Likes

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 :grinning:

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.

Here’s a good reference for your other issue - https://www.restapitutorial.com/httpstatuscodes.html

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.

1 Like

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.

1 Like

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?

> 
> client = system.net.httpClient()
> 
> requests = client.patch(url="https://my.url/motor",
> 	username="myuser", password="mypass",
> 	data={"reading": "1234.5"})
> 
> print requests.json

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.

Thanks for your help everyone

1 Like

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/

Here is an example of doing a GET to https://InductiveAutomation.com. This is the same as your web browser would do.

6 Likes