PepikVaio
PepikVaio

Reputation: 21

Shutdown Kodi with api (blackberry qml)

Hello I trying shutdown Kodi (raspberry pi) with mobile app (blackberry qml). But I do not how.

I used this code: (in browser)

"http://[myip]:[myport]/jsonrpc?request={"jsonrpc":"2.0","method":"System.Suspend","id":1}"

I used this code: (in the app)

function sendRequest() {
    var xhr = new XMLHttpRequest();

    var url = "http://[myip]:[myport]/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\": \"System.Suspend\",\"id\":1}"

    xhr.onreadystatechange = function() {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {

                console.log(xhr.responseText);
                textArea.text = xhr.responseText;
            }
        }
    };
    xhr.open("GET", url, true); // with "POST" I got the same problem.
    xhr.send();
}

I got: {"error":{"code":-32700,"message":"Parse error."},"id":null,"jsonrpc":"2.0"}

Remote from web browser works fine (http://[myip]:[myport]) Thank you for your answers.

********** Update: 21.10.2020 **********

I'm in progress. But I don't know what to do next. I found some information why I have an error. I don't know how to implement in my code.

Can you help me? Thank you so much.

https://github.com/xbmc/xbmc/pull/12281

https://forum.kodi.tv/showthread.php?tid=324598&highlight=json

Here's how to do it.But I can't understand it.

https://retifrav.github.io/blog/2018/09/01/kodi-remote-control-app/

This is my function (on Kodi 17.6 it is working but on Kodi 18 is not working )

function sendRequest() {

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                text.text = xhr.responseText
            }
        }
    };

    var url = 'http://<IP:PORT>/jsonrpc?request={"jsonrpc": "2.0", "id": 1, "method": "System.Shutdown"}'
    xhr.open("GET", url, true) // when I write "POST" - nothing happens
    xhr.send()
}

Upvotes: 0

Views: 234

Answers (1)

MSohm
MSohm

Reputation: 848

You should URL encode the json in your url before it's used in the open method. Use encodeURIComponent() to do that. Your browser is changing:

{"jsonrpc":"2.0","method": "System.Suspend","id":1}"

To:

%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%20%22System.Suspend%22%2C%22id%22%3A1%7D%22

But your code is not.

Upvotes: 0

Related Questions