Page 1 of 1

Sending a POST request with XML data

Posted: Saturday 10 March 2018 17:10
by Charley
Hello I am trying to POST XML data

I have read in the documentation
postData: Optional. When doing a POST this data will be the payload of the request (body). If you provide a Lua table then this will automatically be converted to json and the request-header application/json is set. So no need to do that manually.
Does this mean that my XML is converted to JSON

Code: Select all

       
       local function url_encode(str)
            if (str) then
                str = string.gsub (str, "\n", "\r\n")
                str = string.gsub (str, "([^%w %-%_%.%~])",
                function (c) return string.format ("%%%02X", string.byte(c)) end)
                str = string.gsub (str, " ", "+")
            end
            return str
        end
       
       local press  = '<key state="press" sender="Gabbo">PRESET_4</key>';
       press = url_encode(press)
             
       domoticz.openURL({
            url = 'http://192.168.***.***:8090/key',
            method = 'POST',
            callback = 'Retrieved',
            postData = press
         })

In my call back I receive the following message from item.data: "Error parsing request"

I want to use this to start my Bose Soundtouch (I am not able to get the library that is available working)

Re: Sending a POST request with XML data

Posted: Saturday 10 March 2018 17:50
by dannybloe
Have you tried without the urlencode (btw, there is a url encoder in dzVents (domoticz.utils.urlEncode()) see the docs). And no, xml data is not converted to json by dzVents coz that's far from trivial. Maybe the bose interface also has json support. And you can check the response headers to see what is going on. And.. you can test the api with apps like Postman to see if your request is correct.

Re: Sending a POST request with XML data

Posted: Saturday 10 March 2018 18:13
by Charley
If I dont'use urlEncode then only PRESET_1 is sent als postData. that gave the same error.
Just did a try with the urlEncode but with dzVents (domoticz.utils.urlEncode()) also same reply.

If I go through the BOSE soundtouch documentation there is only XML requests no JSON at all.

2018-03-10 18:37:00.430 dzVents: Info: Cool script: ------ Start internal script: Wekker:, trigger: at 18:37
2018-03-10 18:37:00.431 dzVents: Debug: Cool script: OpenURL: url = http://192.168.178.10:8090/key
2018-03-10 18:37:00.431 dzVents: Debug: Cool script: OpenURL: method = POST
2018-03-10 18:37:00.431 dzVents: Debug: Cool script: OpenURL: post data = %3Ckey+state%3D%22press%22+sender%3D%22Gabbo%22%3EPRESET_4%3C%2Fkey%3E
2018-03-10 18:37:00.431 dzVents: Debug: Cool script: OpenURL: headers = nil
2018-03-10 18:37:00.431 dzVents: Debug: Cool script: OpenURL: callback = Retrieved
2018-03-10 18:37:00.432 dzVents: Info: Cool script: ------ Finished Wekker
2018-03-10 18:37:00.523 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-03-10 18:37:00.735 dzVents: Info: Handling httpResponse-events for: "Retrieved
2018-03-10 18:37:00.735 dzVents: Info: Cool script: ------ Start internal script: Wekker: HTTPResponse: "Retrieved"
2018-03-10 18:37:00.735 dzVents: Info: Cool script: Response is true
2018-03-10 18:37:00.735 dzVents: Info: Cool script: Ontvangen Error parsing request
2018-03-10 18:37:00.736 dzVents: Info: Cool script: ------ Finished Wekker


The error I get from postman is

<?xml version="1.0" encoding="UTF-8" ?>
<errors deviceID="A81B6A28DD44">
<error value="1019" name="CLIENT_XML_ERROR" severity="Unknown">Error parsing request</error>
</errors>

If I send the request with jquery in JavaScript it works

var press = '<key state="press" sender="Gabbo">PRESET_4</key>';
var release = '<key state="release" sender="Gabbo">PRESET_4</key>';
$.ajax({
type :'post',
url : 'http://'+ip+':8090/key',
cache : false,
data : press,
dataType: 'xml',
success :function(xml) {

}
})

Re: Sending a POST request with XML data

Posted: Monday 12 March 2018 12:35
by Charley
The problem has been solved
I had to add a header.

Code: Select all

            local press  = '<key state="press" sender="Gabbo">NEXT_TRACK</key>';
            domoticz.openURL({
                url = 'http://192.168.178.100:8090/key',
                method = 'POST',
                callback = 'Retrieved',
                headers = 'application/xml',
                postData = press
            })
            
If the header is missing there is a parse error. (state and sender are missing)

Re: Sending a POST request with XML data

Posted: Monday 12 March 2018 13:11
by dannybloe
I was just about to suggest that :)

Re: Sending a POST request with XML data

Posted: Monday 12 March 2018 14:17
by Charley
Thanks anyway