JSON-RPC over HTTP?

  Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
bradvido88 Offline
Member+
Posts: 736
Joined: Nov 2008
Reputation: 7
Location: MN
Post: #1
I currently have some custom scripts that update my XBMC library automatically but they are using the (depricated) HTTP API.

I'd like to switch to the JSON-RPC API, but I am having trouble finding an example of how use the interface. I've read the wiki and have enabled the webserver. Browsing to httpL//mybmc/jsonprc tells me that json-rpc is up and running.

So, now I want to use the interface....

  1. Is it possible to get JSON data back by simply browsing to a URL/command such as http://myxbmc/jsonrp?[command] ? If so, can you give me an example [command]?
  2. If this is not possible, what is an easy way to test commands for debugging purposes?

Thanks for the help. I've been reading up on JSON-RPC, but am new to this, so I'm hoping my questions make sense.

XBMC.MyLibrary (add anything to the library)
ForTheLibrary (ForTheRecord & XBMC Library Integration)
SageTV & XBMC PVR Integration
Delete unused thumbs
find quote
_Andy_ Offline
Senior Member
Posts: 128
Joined: May 2010
Reputation: 0
Location: Germany, Milky Way Galaxy
Post: #2
1) No. You have to wrap your json query in a POST request to your desired port.
You can configure the http port in XBMC.
You can use raw port 9090 too. You need to send your request in brackets {} only.

2)I use an simple app in objective c to test the response.
I guess you can find some easy phyton examples which are part of XBMC too.
find quote
darkscout Offline
Posting Freak
Posts: 2,148
Joined: Jul 2008
Reputation: 12
Post: #3
http://boshdirect.com/blogs/tech/set-xbm...brary.html

Or if you just want to use curl.
Code:
curl -i -X POST -d '{"jsonrpc": "2.0", "method": "AudioPlaylist.Add", "params": { "file": "/home/xbmc/Music/Adele - 19/101-adele-daydreams.mp3"}, "id": 1}' http://localhost:9019/jsonrpc
find quote
bradvido88 Offline
Member+
Posts: 736
Joined: Nov 2008
Reputation: 7
Location: MN
Post: #4
Thanks for the replies. I think I will try sending the parameters in a HTTP post. I will test this out tonight.


Thanks

XBMC.MyLibrary (add anything to the library)
ForTheLibrary (ForTheRecord & XBMC Library Integration)
SageTV & XBMC PVR Integration
Delete unused thumbs
find quote
bradvido88 Offline
Member+
Posts: 736
Joined: Nov 2008
Reputation: 7
Location: MN
Post: #5
darkscout Wrote:http://boshdirect.com/blogs/tech/set-xbm...brary.html

Or if you just want to use curl.
Code:
curl -i -X POST -d '{"jsonrpc": "2.0", "method": "AudioPlaylist.Add", "params": { "file": "/home/xbmc/Music/Adele - 19/101-adele-daydreams.mp3"}, "id": 1}' http://localhost:9019/jsonrpc

Ok, I'm using curl on windows. I'm trying to do a test to confirm it works. It is not working. I'm guessing my data syntax in not getting parsed correctly by the command line? Any ideas?
Code:
c:\curl>curl -i -X POST -d '{"jsonrpc": "2.0", "method": "AudioPlayer.PlayPause"}' -u xbmc:xbmc http://localhost:80/jsonrpc
curl: (6) Could not resolve host: 2.0,; No data record of requested type
curl: (6) Could not resolve host: method; No data record of requested type
curl: (3) [globbing] unmatched close brace/bracket at pos 22
HTTP/1.1 200 OK
Content-Length: 115
Content-Type: application/json
Date: Mon, 20 Dec 2010 17:13:39 GMT

{
   "error" : {
      "code" : -32700,
      "message" : "Parse error."
   },
   "id" : 0,
   "jsonrpc" : "2.0"
}

XBMC.MyLibrary (add anything to the library)
ForTheLibrary (ForTheRecord & XBMC Library Integration)
SageTV & XBMC PVR Integration
Delete unused thumbs
find quote
Montellese Online
Team-XBMC Developer
Posts: 2,790
Joined: Jan 2009
Reputation: 20
Location: Switzerland
Post: #6
I never used curl on windows before but there are two things I noticed in your example:

1. Don't set XBMC to provide the JSON-RPC API on port 80 because port 80 is the default port for HTTP requests to the WWW.
2. You're request is missing the "id" field (which you can see in the HTTP response).

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.

[Image: badge.gif]
find quote
bradvido88 Offline
Member+
Posts: 736
Joined: Nov 2008
Reputation: 7
Location: MN
Post: #7
Montellese Wrote:I never used curl on windows before but there are two things I noticed in your example:

1. Don't set XBMC to provide the JSON-RPC API on port 80 because port 80 is the default port for HTTP requests to the WWW.
2. You're request is missing the "id" field (which you can see in the HTTP response).

Thanks for the tips.
1) Is there a way to specify a different port for the JSON-RPC api to listen on. I thought it is always the same port as the web server. Either way, I changed it to 8000 and it did not help.
2) I added and id, but it's not returning it

I think the problem lies in the syntax of my command. curl is not seeing the data as a single object. It seems like its trying to connect to each parameter as though it thinks it's a server by the "Could not resolve host: 2.0" etc. messages.
Code:
c:\curl>curl -i -X POST -d '{"jsonrpc": "2.0", "method": "AudioPlayer.PlayPause", "id": "1"}' -u xbmc:xbmc http://localhost:8000/jsonrpc
curl: (6) Could not resolve host: 2.0,; No data record of requested type
curl: (6) Could not resolve host: method; No data record of requested type
curl: (6) Could not resolve host: AudioPlayer.PlayPause,; No data record of requested type
curl: (6) Could not resolve host: id; No data record of requested type
curl: (3) [globbing] unmatched close brace/bracket at pos 2
HTTP/1.1 200 OK
Content-Length: 115
Content-Type: application/json
Date: Mon, 20 Dec 2010 18:46:37 GMT

{
   "error" : {
      "code" : -32700,
      "message" : "Parse error."
   },
   "id" : 0,
   "jsonrpc" : "2.0"
}
For the -d option, I have also tried
Code:
"jsonrpc=2.0&method=AudioPlayer.PlayPause&id=1"
"{jsonrpc: 2.0, method: AudioPlayer.PlayPause, id: 1}"
"{""jsonrpc"": ""2.0"", ""method"": ""AudioPlayer.PlayPause"", ""id"": ""1""}"
"{'jsonrpc': '2.0', 'method': 'AudioPlayer.PlayPause', 'id': '1'}"
But none have worked successfully

EDIT: I have gotten this to work successfully by telnetting to the raw TCP 9090 port and sending this exact string:
Code:
{"jsonrpc": "2.0", "method": "AudioPlayer.PlayPause", "id": "1"}
I give up on the curl syntax for now.
Thanks to all for the help

XBMC.MyLibrary (add anything to the library)
ForTheLibrary (ForTheRecord & XBMC Library Integration)
SageTV & XBMC PVR Integration
Delete unused thumbs
(This post was last modified: 2010-12-20 21:11 by bradvido88.)
find quote
darkscout Offline
Posting Freak
Posts: 2,148
Joined: Jul 2008
Reputation: 12
Post: #8
It's not curl's problem. It's Window's bastardization of command line parameters.

Notice all the "Could not resolve hosts"? it's trying to look up a 2.0 as a host and do something with it.
find quote
bradvido88 Offline
Member+
Posts: 736
Joined: Nov 2008
Reputation: 7
Location: MN
Post: #9
darkscout Wrote:It's not curl's problem. It's Window's bastardization of command line parameters.

Notice all the "Could not resolve hosts"? it's trying to look up a 2.0 as a host and do something with it.
Yeah, that's why I was trying all the different combinations... trying to get windows to parse it correctly.

maybe a windows cmd line veteran knows what the correct syntax is... or even if a syntax exists that will work.

XBMC.MyLibrary (add anything to the library)
ForTheLibrary (ForTheRecord & XBMC Library Integration)
SageTV & XBMC PVR Integration
Delete unused thumbs
find quote
plumser Offline
Junior Member
Posts: 9
Joined: Mar 2009
Reputation: 0
Post: #10
dunno if this helps any, but i've written a simple python script to update my library. I use this on ubuntu but maybe you can adapt it for windows..
Code:
#!/usr/bin/python
import json
import httplib, urllib

url = 'http://your.xbmc.ip:port'
jsonrpcurl = url + '/jsonrpc'

updatestr = json.dumps({'jsonrpc': "2.0", 'method': "VideoLibrary.ScanForContent", 'id': "1"})

urllib.urlopen(jsonrpcurl, updatestr).close()
find quote
Post Reply