hbrls
hbrls

Reputation: 2150

What is the client for spyne with json document protocol?

I'm trying with the spyne hello world example , the codes basicly are:

class HelloWorldService(ServiceBase):
    @srpc(Unicode, Integer, _returns=Array(Unicode))
    def say_hello(name, times):
        for i in range(times):
            yield 'Hello, %s' % name

application = Application([HelloWorldService],
              tns='spyne.examples.hello',
              in_protocol=JsonDocument(validator='soft'),
              out_protocol=JsonDocument()
          )

if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    wsgi_app = WsgiApplication(application)
    server = make_server('0.0.0.0', 7789, wsgi_app)
    print "rpc server start"
    server.serve_forever()

And I'm trying to connect to it with requests like:

url = "http://127.0.0.1:7789/sayhello"
data = { "name": "World", "times": 4 }
headers = { 'content-type': 'application/json' }
r = requests.post(url, data=json.dumps(data), headers=headers)

It returns with 404.

But if I'm using HttpRpc protocol, the requests way is fine.

So how do I implement a client to use the Json Document protocol. Using the lib requests is preferred.

Upvotes: 2

Views: 1057

Answers (2)

Burak Arslan
Burak Arslan

Reputation: 8001

I've just added request via JsonDocument protocol example to the http://spyne.io.

Check it out: http://spyne.io/#inprot=JsonDocument&outprot=JsonDocument&s=rpc&tpt=WsgiApplication&validator=true

For reference; you can do both:

curl http://localhost:7789 -d '{ "say_hello" : { "name": "World", "times": 4 } }' 

or

curl http://localhost:7789 -d '{ "say_hello" : ["World", 4]}' 

The argument order is the same as the argument order in the Python side.

Upvotes: 1

miguelmiguel19
miguelmiguel19

Reputation: 30

If you want to use JsonDocument as in_protocol, you should use this notation for send data...

data = { "say_hello" : { "name": "World", "times": 4 } }

It means that you should pass your function name as the main key in your json, and its content should be a json with the arguments of your function.

and your url should be the same, but without the function name i.e:

url = "http://127.0.0.1:7789/"

if you want to check more stuff, could read the spyne blog at http://spyne.io/blog/

Upvotes: 1

Related Questions