HTTP Get Function Help

So I am trying to use the system.net.httpGet function to return the saftware version of my IP camera(s). I keep getting a 401 error response and am uncertain as to why.

url = "http://IPAddressofCamera/cgi-bin/magicBox.cgi?action=getSoftwareVersion"
user = 'username'
passwd = 'password'

vers = system.net.httpGet(url, username=user, password=passwd)
print vers

And the response looks like:

Traceback (most recent call last):
  File "<input>", line 5, in <module>
IOError: Server returned HTTP response code: 401 for URL: http://IPAddressofCamera/cgi-bin/magicBox.cgi?action=getSoftwareVersion

When I enter the same address into chrome, it asks for username and password, to which I enter the same user and password as the code. Once this is completed, it displays a webpage with the information I would expect.

Does anyone have any idea as to what could be the problem here?

This function is deprecated for a variety of reasons, including authentication problems. Use system.net.httpClient instead.

1 Like

Thank for the response Phil. However I am still getting the same error.

I have tried the system.net.httpClient a couple different ways, all with the same response.

I started with the following:

url = "http://IPAddress/cgi-bin/magicBox.cgi?action=getSoftwareVersion"
user = 'username'
passwd = 'password'

client = system.net.httpClient(username=user, password=passwd, bypass_cert_validation = True,cookie_policy = "ACCEPT_ALL",redirect_policy="ALWAYS")
vers = client.get(url)

print vers

and I get a response of:

>>> 
<Response@1882423925 'http://IPAddress/cgi-bin/magicBox.cgi?action=getSoftwareVersion' [401]>
>>> 

So I then tried putting the user and password as part of the client.get and still get the same response.

url = "http://IPAddress/cgi-bin/magicBox.cgi?action=getSoftwareVersion"
user = 'username'
passwd = 'password'

client = system.net.httpClient(bypass_cert_validation = True,cookie_policy = "ACCEPT_ALL",redirect_policy="ALWAYS")
vers = client.get(url, username=user, password=passwd)

So obviously I am doing something wrong and am uncertain of what it is. Does anyone have any insights?

My guess would be that the camera doesn’t take HTTP Basic Auth, which is what httpClient is passing in.

Error 401 is an unauthorized error.

You could certainly print out the text of the response as well. It might give you a little more info:

print vers.getText()
(For other functions available for the Response object, see the docs in https://docs.inductiveautomation.com/display/DOC80/system.net.httpClient )

Since this is less a post about Ignition and more a post about REST communication, my recommendation would be to use a free tool like PostMan ( https://www.postman.com/ ) to make sure you get your REST calls right. It has a nice interface that lets you test communication intended exactly for working out this type of request. After you have that working, then you can throw the right settings and options into Ignition’s httpClient() call.

In your requests you might end up needing to set some cookies, pass in authentication using something other than HTTP Basic Auth (using a post possibly with form data, and getting back a token, for example). If you want to watch how the browser does it, you can open up Chrome’s Dev Tools ( Ctrl-Shift-I ) and watch the Network tab. It has details on the headers and data in the requests and responses.

Hope this helps!

2 Likes

@Kuskah, were you able to solve your issue? I’m running into the same problem with my cameras due to an unsecure http secondary message. Any insight into your solution would be greatly appreciated. Thanks