Google Groups

ServiceProxy


hannes.koller Feb 29, 2012 1:31 AM
Posted in group: JSON-RPC

Hi,

I ran into a problem with the ServiceProxy class yesterday.  I was trying to connect to a Service but all I got were errors of the kind "415 Unsupported Media Type". After some poking around I found out that the ServiceProxy does not set the "Content-Type" header of the request. I wrote a small patch to remedy the problem and thought maybe this could be of interest so here it goes:

<code>

import urllib2  # I had to use urllib2 instead of urllib  (http headers)


class ServiceProxy(object):

def __call__(self, *args):

  postdata = dumps({"method": self.__serviceName, 'params': args, 'id':'jsonrpc'})

 

  #

  # BEGIN patch


  req = urllib2.Request(url=self.__serviceURL, data=postdata)

  req.add_header("Content-Type", "application/json")

  respdata = urllib2.urlopen(req).read()


  # END patch

  #


  resp = loads(respdata)

  .....


</code>


Cheers,
Hannes :)