HTTP GET request scripting

Hi all,

I’m once again pushing my Ignition knowledge limits trying to do HTTP requests via an API for an external website.

I have found the resource on Ignition function system.net.httpGet: https://docs.inductiveautomation.com/display/DOC/system.net.httpGet

And that put me in the right direction, but I’m still not sure how to properly handle the result data.
If the server returns an HTTP error status code, how can i extract that and log the appropriate error message?

My code so far:

[code]
import system

Generate URL for API call to read products

url = “https://inventory.dearsystems.com/ExternalApi/Products?Page=1&Limit=100&Name=Log

Create header with information required by API

header = {“Content-type”:“application/json”, “api-auth-accountid”:“XXXX-XXXX”, “api-auth-applicationkey:”:“XXXX-XXXX”}

try:
source = system.net.httpGet(url,’’ ,’’ ,’’,’’,header)
print source
except:
print(“Error calling DEAR API”)[/code]

I don’t think i have quite set up the headers correctly, the reference document for the API i’m trying to call is in PHP, and i have no experience in PHP so might be misinterpreting it.
This is the example given to connect to the API: http://support.dearsystems.com/support/solutions/articles/1000054245-connecting-to-the-api

Any help on creating an http request and dealing with the returned data would be much appreciated.

1 Like

Solved using the following code:

[code]import system
import urllib2

Generate URL for API call to read products

url = “https://inventory.dearsystems.com/ExternalApi/Products?Page=1&Limit=100&Name=Log

Create header with information required by API

urlHeader = {“Content-Type”: “application/json”, “api-auth-accountid”: “XXXXX”, “api-auth-applicationkey”: “XXXXX”}

request = urllib2.Request(url, headers=urlHeader)
contents = urllib2.urlopen(request).read()

data = system.util.jsonDecode(contents)[/code]

1 Like

Just as a note if anyone comes to this for API help, it is probably better to use Ignitions function system.net.httpGet() instead of urllib2.
I have had less issues this way.

1 Like

Hello,

I’m new to Ignition. Can you please tell me how to install requests module to Ignition? Currently, when I import requests in the script console. I got the error message: no requests module found.

Many thanks!

Min

Hi,

Download requests module from here: https://pypi.python.org/pypi/requests/0.2.4
Copy to your ignition server and extract files.
Install by following these instructions: https://support.inductiveautomation.com/index.php?/Knowledgebase/Article/View/98/0/importing-and-using-3rd-party-python-libraries-in-ignition

1 Like

Thank you very much, Richard

Many thanks

Min