[PATCH] ServiceProxy fails with 'KeyError' on JSONRPC-2.0 compliant response

174 views
Skip to first unread message

chris

unread,
Nov 17, 2011, 4:42:00 AM11/17/11
to JSON-RPC, christo...@gmail.com
I used the ServiceProxy class as a client to a JSON-RPC server from
jsonrpclib (https://github.com/joshmarshall/jsonrpclib). It fails
with:

Traceback (most recent call last):
File "./rpcclient.py", line 9, in <module>
print(s.reverse('foobar'))
File "/usr/lib/python2.7/site-packages/jsonrpc/proxy.py", line 44,
in __call__
if resp['error'] != None:
KeyError: 'error'

The TCP stream looks as follows:

POST /RPC2 HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 55
Host: localhost:8000
User-Agent: Python-urllib/1.17

{"params":["foobar"],"method":"reverse","id":"jsonrpc"}

HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.2
Date: Thu, 17 Nov 2011 09:36:52 GMT
Content-type: application/json-rpc
Content-length: 55

{"jsonrpc": "2.0", "result": "raboof", "id": "jsonrpc"}

According to the specification of JSON-RPC-2.0 (http://www.jsonrpc.org/
spec.html), the error member must not exist if there was no error:

5 Response object
[...]
error
This member MUST NOT exist if there was no error triggered during
invocation.
[...]

The following patch fixes the behaviour:

--- jsonrpc/proxy.py.orig 2011-11-16 11:44:06.145643756 +0200
+++ jsonrpc/proxy.py 2011-11-16 11:45:27.198537988 +0200
@@ -41,7 +41,7 @@
postdata = dumps({"method": self.__serviceName, 'params':
args, 'id':'jsonrpc'})
respdata = urllib.urlopen(self.__serviceURL,
postdata).read()
resp = loads(respdata)
- if resp['error'] != None:
+ if 'error' in resp and resp['error'] != None:
raise JSONRPCException(resp['error'])
else:
return resp['result']


Please consider applying.

Thanks,
Chris

Matt (MPCM)

unread,
Nov 17, 2011, 9:05:01 PM11/17/11
to JSON-RPC
Contact the author of the library you are using directly, in
particular since it is on git hub. :)

Also, looks your object is missing this as well:

"jsonrpc": "2.0",

from
> {"params":["foobar"],"method":"reverse","id":"jsonrpc"}


--
Matt (MPCM)

Christoph Gysin

unread,
Nov 18, 2011, 2:39:49 AM11/18/11
to json...@googlegroups.com
My fault, didn't realise that this list is not specific about the
library I'm talking about.

The client library I'm using is described here:
http://json-rpc.org/wiki/python-json-rpc

> Contact the author of the library you are using directly, in
> particular since it is on git hub. :)

That's what I'm trying to do. This list is supposed to be contacted
for questions about python-json-rpc.

> Also, looks your object is missing this as well:
>
> "jsonrpc": "2.0",
>
> from
>> {"params":["foobar"],"method":"reverse","id":"jsonrpc"}

That would be again the clients fault. So it seems it's not very 2.0
compliant at all.

Well, never mind then. The patch made the client work for me

Chris
--
echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3

David Aguilar

unread,
Jul 8, 2012, 2:16:45 AM7/8/12
to json...@googlegroups.com
Hi Chris

In case you haven't seen it, I've been maintaining a fork of the
aforementioned python code that you might be interested in.

https://github.com/davvid/python-jsonrpc

I've been using it in with python's CGIServer for testing and apache /
mod_python for production and it's been working just fine.

It doesn't have the bug you mention above, as it avoids it in a
similar way. It also uses the correct "jsonrpc": "2.0" data in the
calls.
--
David
Reply all
Reply to author
Forward
0 new messages