HTTP POST Request help needed

Hi! I try send notifications to LaMetric Time (I know about binding, but it did not provide icons in notifications)
Here is curl command:

curl -X POST -u "dev:xxxx" -H "Content-Type: application/json" -d " { \"model\": { \"frames\": [ { \"icon\":\"a2867\", \"text\":\"Hello\"} ] } }" http://192.168.1.80:8080/api/v2/device/notifications

I try and no luck:

executeCommandLine(Duration.ofSeconds(6), "/bin/sh", "-c", "'curl -X POST -u \"dev:xxxx\" -H \"Content-Type:application/json\" -d \"{\"model\":{\"frames\":[{\"icon\":\"a2867\",\"text\":\"Hello\"}]}}\" http://192.168.1.80:8080/api/v2/device/notifications'")

executeCommandLine(Duration.ofSeconds(6), ("curl -X POST -u 'dev:xxxxx' -H 'Content-Type:application/json' -d '{\"model\":{\"frames\":[{\"icon\":\"a2867\",\"text\":\"Hello\"}]}}' http://192.168.1.80:8080/api/v2/device/notifications").split(" "))

Read DOCs, and not found how to send -u parameter from curl in openHAB sendHttpPostRequest HTTP Actions

Why don‘t use sendHttpPostRequest?

I just checked my code and I don‘t use -u at all

Need to provide user and pass in request

-u "dev:xxxx"

I dont know how

sendHttpPostRequest(String url, String contentType, String content, Map<String, String> headers, int timeout)

Where to put -u?

You can try the code below - just change your ApiKey, ip-address and the json-data:

val String LaMetric_ApiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
val String LaMetric_Notification_Endpoint = "http://dev:" + LaMetric_ApiKey + "@192.168.100.50:8080/api/v2/device/notifications"
val String LaMetric_Notification_Headers = "application/json"
var String LaMetric_Data = "{your json here}"
var String API_RETURN = sendHttpPostRequest(LaMetric_Notification_Endpoint,LaMetric_Notification_Headers,LaMetric_Data,20000)

Yes! Thank you @Flip :grinning:

Is it working?

Yes

Well done :slight_smile: Maybe you can mark the code above as solution - just in case somebody else is having this problem

Did it already :slightly_smiling_face:

1 Like