[SOLVED] How to use sendHttpPostRequest with HTTP Headers

Hello

I’ve a problem with rules and sending an HTTP Request. If I send the following HTTP Request:

var url = "http://localhost:8888"
var String content = '{"from" : "OpenHAB", "to" : "Email_ADDRESS", "text" : "Test Message"}'
var response = sendHttpPostRequest(url, "application/json", content, 3000)

I receive this at the server:

POST / HTTP/1.1
Accept-Encoding: gzip
Content-Type: application/json
Host: localhost:8888
Content-Length: 69

{"from" : "OpenHAB", "to" : "Email_ADDRESS", "text" : "Test Message"}

As I need to add additional HTTP Headers I need the sendHttpPostRequest with addtional headers as shown in the online documentation (https://www.openhab.org/docs/configuration/actions.html):
sendHttpPostRequest(String url, String contentType, String content, Map<String, String> headers, int timeout)

I added the header in the code and adapted the sendHttpPostRequest

var headers = newHashMap("client_id" -> "1234567890abcdef", "API-Version" -> "2")
var response = sendHttpPostRequest(url, "application/json", content, headers, 3000)

In this case I received the error of too many arguments for the function. Error Message:
2020-08-31 15:07:00.118 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Send Alert': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.HTTP.sendHttpPostRequest(java.lang.String,java.lang.String,java.lang.String,int) on instance: null

Why do I receive this error?

The online docs have an unfortunate habit of showing you the latest version.
If you select version 2.5 (top right) you will find the HTTP Actions do not support headers.

You"d need openHAB 3

thanks for the information. this information would have saved me hours of testing. I see that openhab 3.0 is not released and might take some month. do you know a work a round?

is there a way to send http requests with additional specific http headers?

I submitted the PR for the http headers. For 2.5 you can use this jar to replace your copy: https://github.com/jimtng/binaries/blob/master/org.openhab.core.model.script-2.5.0.jar

This will allow you to use it without waiting for 3.0

2 Likes

Thanks for the jar file. stupid question: where do I need to put this file? do you have a path for linux?

If your openhab installation is directly on linux (not docker), just find the similarly named file and rename the original to .org and put this file in its place. From memory, I think you won’t even need to restart openhab but you could to be sure.

In docker, it’s located here: /openhab/runtime/system/org/openhab/core/bundles/org.openhab.core.model.script/2.5.0/org.openhab.core.model.script-2.5.0.jar

and I replaced mine using a volume mount, like this:

    volumes:
      - /home/openhab/tmp/org.openhab.core.model.script-2.5.0.jar:/openhab/runtime/system/org/openhab/core/bundles/org.openhab.core.model.script/2.5.0/org.openhab.core.model.script-2.5.0.jar

Note that an upgrade will wipe out this file, so keep a copy of the new jar locally and try to remember to replace it after each upgrade.

thanks for the file and the path. I needed to place it in /usr/share/openhab2/runtime/system/org/openhab/core/bundles/org.openhab.core.model.script/2.5.0/

I changed the owner to openhab and checked the permissions. but unfortunately I still getting the following error:
2020-09-01 08:10:00.558 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Send Alert': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.HTTP.sendHttpPostRequest(java.lang.String,java.lang.String,java.lang.String,int) on instance: null

Are you sure that it is implemented in this new file?

Additionally did I everything correct with the initialisation of headers? that it is a Map<String,String>
var headers = newHashMap("client_id" -> "1234567890abcdef", "API-Version" -> "2")

Comment - HTTP binding v2.x under development offers header controls, if you want a more conventional approach.
Binding applications not quite the same as Action applications, depending on your usage here.

I’m fairly certain - from memory I did test it back then and have been using it.

It’s a strange error message. The signature of the method is wrong - sendHttpPostRequest(String, String, String, int)? It should be (String, String String, Map<String, String>, int). Furthermore, I don’t understand what this means:

… on instance: null

Try stopping openhab, clearing the cache and starting it up again.

OK I’ve tested this again just now. The JAR file works. You could even just drop it off into the addons folder, without renaming the original jar to .org. So it won’t get overwritten when you upgrade openhab - but you need to remember to delete it when you upgrade to OH3.

When you put it in the addons folder, you won’t need to clear the cache / tmp. If you replaced the original file (instead of putting it in the addons folder), you will need to clear your tmp+cache and restart openhab - possibly twice, i.e. restart, wait for a few minutes, restart again.

About this as you want to use it as an action from a rule. I had the same issue a year back or so and am now using another method for sending http get, post etc with headers,… This also enables you to catch some exceptions and act on it.
I don’t have it on my phone, but I used the code from the thread "OAuth 2.0"and just modified it a bit. Works great for my rules and I can catch some more exceptions and rerun some functions then :slight_smile: you can give it a try as well

thanks for your support. Stopping, clearing cache and starting again. solved my issue. Thanks all for the great support.

Does this work for 2.5.12?

I just tried this both by replacing the file in /usr/share/openhab2/runtime/system/org/openhab/core/bundles/org.openhab.core.model.script/2.5.0
and received the error Resource mvn:org.openhab.core.bundles/org.openhab.core.model.script/2.5.0 does not contain a manifest

I then tried instead to just drop it into /usr/share/openhab2/addons which did not work. I still receive the error: Could not invoke method: org.eclipse.smarthome.model.script.actions.HTTP.sendHttpPostRequest(java.lang.String,java.lang.String,java.lang.String,int)

no combination of restarting or cache cleaning seemed to get anywhere

thanks!

Hi @novahawk13 sorry I have upgraded my system to oh3 and no longer have a 2.5 to test it on. At the time I posted, it did work on 2.5. perhaps the jar didn’t get loaded by openhab? Try doing

bundle:list -s | grep http

Hey, apologies myself, I also forgot to reply to this. I decided it was time to just upgrade to OH3 and all is working now.

Thanks!

1 Like