POST to external API from a rule

I have a Reolik survelance camera, and I want to change position of the camera by sending a post message from within a rule to its API.

POST: http://192.168.2.39/cgi-bin/api.cgi?cmd=PtzCtrl&token=MyTokenxxx
with the json payload: [{“cmd”:“PtzCtrl”,“action”:0,“param”:{“channel”:0,“op”:“ToPos”,“speed”:32,“id”:1}}]

will move the camera to preset position #1.

How can I include this Post and payload in a rule?

Thanks!!!

However, I do not get it to work, I guess that I made something wrong with the ReolinkPayload?

		var String ReolinkString
		val String ReolinkPost = "http://192.168.2.39/cgi-bin/api.cgi?cmd=PtzCtrl&token=MyRokenxxx"
		val String ReolinkPayload = [{"cmd":"PtzCtrl","action":0,"param":{"channel":0,"op":"ToPos","speed":32,"id":1}}]
		sendHttpPostRequest(ReolinkPost, ReolinkPayload, ReolinkString )
		val String ReolinkPayload = "[{\"cmd\":\"PtzCtrl\",\"action\":0,\"param\":{\"channel\":0,\"op\":\"ToPos\",\"speed\":32,\"id\":1}}]"

	var String ReolinkString ="[{\"cmd\":\"PtzCtrl\",\"action\":0,\"param\":{\"channel\":0,\"op\":\"ToPos\",\"speed\":32,\"id\":1}}]"
	val String ReolinkPost = "http://192.168.2.39/cgi-bin/api.cgi?cmd=PtzCtrl&token=MyRokenxxx"
	val String ReolinkPayloadType = "application/json"
	sendHttpPostRequest(ReolinkPost, ReolinkPayloadType, ReolinkString )

It worked!! Thanks! However, I got a lot of:
2018-09-01 16:36:16.466 [ERROR] [.smarthome.model.script.actions.HTTP] - Fatal transport error: java.util.concurrent.TimeoutException
but if I executed the rules twice it did the second time without error. Very strange.

However, it turned out that the token expires and I have to renew it. I can get it to post the json

   {
  "cmd" : "Login",
  "code" : 0,
  "value" : {
     "Token" : {
        "leaseTime" : 3600,
        "name" : "7224971121357e8"
     }
  }

}
]

But I have not manage to extract the token (“name”). I guess that my json path is wrong?

		var String ReolinkString ="[{\"cmd\":\"Login\",\"action\":0,\"param\":{\"User\":{\"userName\":\"admin\",\"password\":\"MyPassword\"}}}]"
		val String ReolinkPost = "http://192.168.2.39/cgi-bin/api.cgi?cmd=Login&token=null"
		val String ReolinkPayloadType = "application/json"
		var String ReolinkToken = sendHttpPostRequest(ReolinkPost, ReolinkPayloadType, ReolinkString )
		var String Json_test = transform( "JSONPATH",  "$.value.Token.name", ReolinkToken )
		
	logInfo("Reolink ", ReolinkToken)
	logInfo("Reolink Token ", Json_test)

That is because the returned String is NOT a valid JSON
Try:

    logInfo("Reolink ", ReolinkToken)
    var String Json_test = ReolinkToken.substringBetween("[", "]") // Remove first pair of "[ ]"
    Json_test = Json_test.substringBetween("{", "}") // Remove first pair of "{ }" Now we have a valid JSON
    Json_test = transform( "JSONPATH",  "$.value.Token.name", Json_test )
    logInfo("Reolink Token ", Json_test)

Did that work?

It worked, but it still very unreliable. Some time I get these error messages:
2018-09-01 22:10:01.025 [ERROR] [.smarthome.model.script.actions.HTTP] - Fatal transport error: java.util.concurrent.TimeoutException
2018-09-01 22:10:03.055 [INFO ] [pse.smarthome.model.script.Reolink 1] - null

I tried to run the script 3 time in the hope that it would work more reliable

rule "UpdateReolinkToken"
when
	Time cron "0 0/5 * 1/1 * ? *" 
then	
		var String ReolinkString ="[{\"cmd\":\"Login\",\"action\":0,\"param\":{\"User\":{\"userName\":\"admin\",\"password\":\"MyPassword\"}}}]"
		val String ReolinkPost = "http://192.168.2.39/cgi-bin/api.cgi?cmd=Login&token=null"
		val String ReolinkPayloadType = "application/json"
		var String RelolinkToken = sendHttpPostRequest(ReolinkPost, ReolinkPayloadType, ReolinkString )
		Thread::sleep(2000)
		logInfo("Reolink 1", RelolinkToken)
			
		RelolinkToken = sendHttpPostRequest(ReolinkPost, ReolinkPayloadType, ReolinkString )
		logInfo("Reolink 2", RelolinkToken)
		Thread::sleep(2000)	
		RelolinkToken = sendHttpPostRequest(ReolinkPost, ReolinkPayloadType, ReolinkString )
		logInfo("Reolink 3", RelolinkToken)
	    Json_ReolinkToken = RelolinkToken.substringBetween("[", "]") // Remove first pair of "[ ]"
    	Json_ReolinkToken = transform( "JSONPATH",  "$.value.Token.name", Json_ReolinkToken )
    	logInfo("Reolink Token ", Json_ReolinkToken)
end

Any ideas how I can eliminate the error?