Other data formats

If you can’t use SIO, JSON or XML your services can still accept and return data of any type provided they’re not so called binary data - such information needs to wrapped in BASE64 or a similar encoding.

First let’s create an empty service. Save the file as other_example.py and hot-deploy it. Next, add a channel to expose the service over. Note that the channel doesn’t specify any particular data format.

1
2
3
4
5
from zato.server.service import Service

class MyService(Service):
    def handle(self):
        pass
../_images/other-channel1.png
  • Now that the channel has been created, we can modify the service to handle CSV. This could be any other format but let's use CSV because of its clear syntax.

    The raw string a client passes in is available as self.request.payload. In the case below, we can process it using Python's built-in csv module.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    # stdlib
    import csv
    
    # Zato
    from zato.server.service import Service
    
    class MyService(Service):
        def handle(self):
            reader = csv.reader(self.request.payload.split(';'))
            for line in reader:
                if line:
                    self.logger.info('Got elements: {}'.format(line))
    
    $ cat data.csv
    q,w,e;
    r,t,y;
    u,i,o;
    $
    $ curl localhost:17010/other-example.my-service -d @data.csv
    INFO - Got elements: ['q', 'w', 'e']
    INFO - Got elements: ['r', 't', 'y']
    INFO - Got elements: ['u', 'i', 'o']
  • To return a response, assign a value to self.response.payload. If using a custom data format, however, you will most likely prefer to use appropriate content type too, like in the Short Payment Descriptor example below.

    1
    2
    3
    4
    5
    6
    from zato.server.service import Service
    
    class MyService(Service):
        def handle(self):
            self.response.payload = 'SPD*1.0*ACC:CZ2408000000190000000123*MSG:My debts'
            self.response.content_type = 'application/x-shortpaymentdescriptor'
    
    $ curl localhost:17010/other-example.my-service
    SPD*1.0*ACC:CZ2408000000190000000123*MSG:My debts

Simple IO (SIO)

Note

Again, please consider using SIO if you're developing a new application or its existing data model allows you to.