[SOLVED] HTTP request with Credentials

Is it possible to do an HTTP request with the binding AND provide credentials? I’ve looked at http://docs.openhab.org/addons/bindings/http1/readme.html but there doesn’t seem to be any mention of credentials.

I have a URL I would normally use CURL for like this :

curl --user jeff@exampledomain.com:password123 https://api.companyx.com/device/10/

Doesn’t seem possible with the HTTP binding unless I’ve missed something?
(And yes the username has the @ symbol in, which adds to the fun :slight_smile: )
The above URL would normally return basic xml as below and I’m after the numeric value of “Level”.

<device>
	<physical>
		<status>Idle</status>
		<cP>0.0</cP>
		<Level>4</Level>
		<critical>false</critical>
	</physical>
</device>

Hi Jeff,

Yes it is possible. There is a very good article on Wikipedia for Basic access authentication.

You can realize the HTTP request with credentials in your configuration in two ways:

String Test {
    http="<[http://username:password@sample.com]"
}

When your username or password contains reserved characters like an @ symbol you should encode the username and password in Base 64 and use a header, where the headers’s value is the base64-encoding of username:password (results in dXNlcm5hbWU6cGFzc3dvcmQ=). There is an online decoder/encoder here.

String Test {
    http="<[http://sample.com{Authorization=Basic dXNlcm5hbWU6cGFzc3dvcmQ=}]"
}

I will add this explanation to the HTTP documentation.

1 Like

Hello @cweitkamp!

I couldn’t get Basic Auth in combination with JSON running directly in an item definition. The only way it worked for me was like described here: Http binding question - cache not working

Regards,
Herbert

Hi Herbert,

could you be so kind and post your not working item definition? I try to look into this later this day.

Hi Christoph!

Working:

http.cfg


# configuration of the first cache item

vpm.url=http://192.168.1.XX:80/wizard/public/api/measurements{Authorization=Basic dXNlcjEyMzQ1Njc4OQ==}
vpm.updateInterval=5000

Item

Number 	meterPT		"Gesamtleistung [%.0f W]"	<meter> 	{http="<[vpm:5000:JS(PT.js)]"}

Not working:

Number 	meterPT1		"Gesamtleistung [%.0f W]"	<meter> 	{http="<[http://192.168.1.XX:80/wizard/public/api/measurements:5000:JS(PT.js){Authorization=Basic dXNlcjEyMzQ1Njc4OQ==}]" }
Number 	meterPT2		"Gesamtleistung [%.0f W]"	<meter> 	{http="<[http://dXNlcjEyMzQ1Njc4OQ==@192.168.1.XX:80/wizard/public/api/measurements:5000:JS(PT.js)]" }

Of course credentials above are not the real ones.

There is a small syntax error in your item definition. The headers have to be located directly after the url, before the milliseconds.

Number 	meterPT1		"Gesamtleistung [%.0f W]"	<meter> 	{http="<[http://192.168.1.XX:80/wizard/public/api/measurements{Authorization=Basic dXNlcjEyMzQ1Njc4OQ==}:5000:JS(PT.js)]" }
1 Like

Thank you @cweitkamp - working.

Probably it would be helpful for other noobs to put a full example with BasicAuth in the http Binding docu (http://docs.openhab.org/addons/bindings/http1/readme.html)
At least for me it wasn’t clear where to put the credentials.

Glad to hear. No problem. I will have a look at the documentation.

I probably got the simular issue. The server I am trying to request is ccu.sh, documentation provides example of request. h ttps://ccu.sh:8080/data.cgx?cmd={“Command”:“GetStateAndEvents”}
It contains json format data. Could you help with the proper binding or item definition that incorporates basic authentification?