Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Adding Authorization header to HttpGet() request


WadeS Sep 17, 2013 03:03 AM

We have a StarDot NetCam SC connected to the NL115 Ethernet interface on our CR3000 logger, and we are successfully using HttpGet() to retrieve still images at 30-minute intervals, with LoggerNet downloading them on a schedule. We would also like to issue some command/control instructions to the NetCam for things like revising the overlay text and enabling/disabling the IR filter. However, those commands in the StarDot API require basic HTTP authentication and we haven't been able to get that to work from CR-BASIC.

I tried using the deprecated "http:/ip/username:password@url" syntax, but that didn't work (camera's web server returned an invalid request error). Then I tried passing in an authorization string as a custom header in the call to HttpGet() using the standard syntax: "Authorization: Basic YWRtaW46YWRtaW4=", where "YWRtaW46YWRtaW4=" is the base64-encoded username:password for the camera (admin:admin)

Here's the relevant CR-BASIC code:

HGetResp = "USR:gcesapelo.jpg" 'file location
HGetHead = "Authorization: Basic YWRtaW46YWRtaW4="
HGetHandle = HTTPGet("http://192.168.1.2/netcam.jpg",HGetResp,HGetHead)

Unfortunately that returns a 401 unauthorized error. The documentation for HttpGet() is pretty sparse in the CR3000 manual - what are the format requirements for specifying a custom HTTP header? Do I need to add a line terminator (\r\n)? Advice would be appreciated.

* Last updated by: WadeS on 9/16/2013 @ 9:04 PM *


aps Sep 17, 2013 08:27 AM

Not an expert on this myself but please see this thread as there is a very similar example posted by Sam:

http://www.campbellsci.com/forum/messages.cfm?threadid=1AECB84D-A609-DBB8-6AB1D57E9B8223B1


WadeS Sep 17, 2013 01:07 PM

Thanks - I tried adding the additional header fields used in the socket example in that older thread you pointed out, but still got a 401-unauthorized response. Here's what I used for the header:

HGetResp = ""
HGetHead = "Host: http://localhost" + CHR(13)+CHR(10)
HGetHead = HGetHead + "User-Agent: Mozilla/4.0" + CHR(13)+CHR(10)
HGetHead = HGetHead + "Authorization: Basic xxx" + CHR(13)+CHR(10) 'login removed
HGetHandle = HTTPGet("http://192.168.1.100/admin.cgi?overlay&overlay_utc=0&overlay_scale=1",HGetResp,HGetHead)

Is there any formal documentation for the HttpGet() command other than what's in the CR3000 manual, which just lists arguments without any examples? I haven't been able to find it in other PDFs on the resource disk.


aps Sep 17, 2013 01:19 PM

Your will find more detailed help and examples in the help system for the CRBasic editor. However, the example for HTTPGET is rather simple.

Below is another example that did work in a non-camera application.

httpHead1 = "Authorization: Basic dXN3cjpzb1x4cw=="
httpHead1 = httpHead1 + CHR(13) + CHR(10)
HTTPResult1 = HTTPGet("http://172.254.3.241/logarchive.php", "CRD:LogArcv.dat", httpHead1)

Please make sure you have a recent operating system in the logger as there have been changes to the requirements to needing to open and close sockets (now generally not needed).


WadeS Sep 17, 2013 01:37 PM

Thanks - I'll look at the CRBasic editor docs. Must have missed that.

I'll try that exact formatting for the header, but it's looking like the code I've tried should have worked. I'll check with StarDot and see if I can get any information from them on authentication requirements for non-browser use of the API.

I appreciate your help.

Wade


WadeS Sep 17, 2013 04:14 PM

To follow up, the syntax pattern in the last example worked!

httpHead1 = "Authorization: Basic dXN3cjpzb1x4cw=="
httpHead1 = httpHead1 + CHR(13) + CHR(10)
HTTPResult1 = HTTPGet("http://172.254.3.241/logarchive.php", "CRD:LogArcv.dat", httpHead1)

I can now successfully issue admin commands to the camera. Thanks for pointing out that example. In the end it was the lack of line terminator that buggered the header (CHR(13) + CHR(10)), because I tried that exact syntax without the terminator yesterday. So it probably came down to how the /get request was formatted by HTTPGet().


Sam Sep 24, 2013 03:02 AM

One of the best ways to catch these types of errors, i.e. "is my header formatted correctly and does it contain the right type of information?" is to watch your traffic using a terminal emulator.

At the command line choose W to "watch" a port. Then select port 13 for IP. Try watching the traffic in ASCII and then HEX. You should be able to easily tell if you have enough CRLF's.

Log in or register to post/reply in the forum.