Kodi Community Forum

Full Version: Trouble getting JSON-PRC to work with Python requests.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there. The folowing code just gets me all XBMC parameters instead of movies, what am i missing?
PHP Code:
import requests
import json


url 
"http://localhost:8080/jsonrpc"
headers = {'content-type''application/json'}

payload = {"jsonrpc""2.0""method""VideoLibrary.GetMovies""params": { "filter": {"field""playcount""operator""is""value""0"}, "limits": { "start" 0"end"75 }, "properties" : ["art""rating""thumbnail""playcount""file"], "sort": { "order""ascending""method""label""ignorearticle"'true' } }, "id""libMovies"}

    
requests.get(urlparams=payloadheaders=headers);
print(
r.json()); 
You need to use HTTP POST or pass the JSONRPC request with the "request" parameter.
Hi, Did you get this to work? Trying to do same thing
Thanks
Haven't tried it myself, but looks promising: https://github.com/jcsaaddupuy/python-xbmc
(2015-02-12, 07:19)Montellese Wrote: [ -> ]You need to use HTTP POST or pass the JSONRPC request with the "request" parameter.

Thank you Montellese for replying. I'm back from a quick 10 days vacation. I did a POST request and got a parse error.

Perhaps Python is playing with me?

Code:
import requests
import json


url = "http://localhost:8080/jsonrpc";
headers = {'content-type': 'application/json'};

payload = {"jsonrpc": "2.0",
           "method": "VideoLibrary.GetMovies",
           "params": {"filter": {"field": "playcount", "operator": "is", "value": "0"},
                      "limits": { "start" : 0, "end": 75 },
                      "properties" : ["art", "rating", "thumbnail", "playcount", "file"],
                      "sort": { "order": "ascending", "method": "label", "ignorearticle": 'true' } },
           "id": "libMovies"};


r = requests.post(url, data=payload, headers=headers);

print(r.json());

Returns:

Code:
{'jsonrpc': '2.0', 'id': None, 'error': {'code': -32700, 'message': 'Parse error.'}}
your payload is invalid.

use http://jsonlint.com/ to validate
Just removed the single quotes on 'true' and replaced them by double quotes, was a typo, still getting the parse error.

The thing is, the same thing in NodeJS works.

Edit:

mmmh, checked how the url is assembled by Python Requests and got:

Code:
http://localhost:8080/jsonrpc?jsonrpc=2.0&id=libMovies&params=sort&params=properties&params=filter&params=limits&method=VideoLibrary.GetMovies

Looks like somehow the parameters are not passed by Requests.
(2015-02-24, 23:56)GAZ082 Wrote: [ -> ]Just removed the single quotes on 'true' and replaced them by double quotes, was a typo, still getting the parse error.
There are no quotes of any kind around true/false.

(2015-02-24, 23:56)GAZ082 Wrote: [ -> ]
Code:
http://localhost:8080/jsonrpc?jsonrpc=2.0&id=libMovies&params=sort&params=properties&params=filter&params=limits&method=VideoLibrary.GetMovies

Looks like somehow the parameters are not passed by Requests.
Yup that looks completely wrong.
Instead of using payload as a Python dictionary:

python:
payload = { "jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "mybash"}

Use it as a single string:

python:
payload = '{ "jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "mybash"}'

Now the parse error is gone. I know this post is old, but maybe someone with a similar problem will benefit from it.