HTTP outgoing connections

Overview

Synchronously invokes a remote HTTP server using JSON, SOAP or any other transport method using previously configured connection parameters.

If the connection has been configured to use a security definition, it will be picked up automatically, without any coding. Likewise, if the service to be invoked is a SOAP one, only the contents of a SOAP body should be passed to .post and the envelope will be added by Zato.

Requests are sent in a synchronous manner but they don’t block the current server thread.

The underlying client HTTP library is Requests.

Each request sent using one of the .ping, .get or .post methods will contain 3 Zato-specific HTTP headers:

Name Example Notes
X-Zato-CID K294883017331181958264688131768546402685 CID the service issuing the request has been invoked with
X-Zato-Component parallel/esb1/esb1.example.com/21358/Thread-4 Information regarding which Zato parallel server issued the request
X-Zato-Msg-TS 2013-04-26T23:41:45.271911 Request timestamp (in UTC)

API

self.outgoing.plain_http.get

get(name):

Fetches an object whose .conn attribute represents a connection to an HTTP server invoked through plain HTTP, including JSON.

Parameters:name (string) – Name of the HTTP connection template
Return type:(A thin wrapper around a connection to the server)

self.outgoing.soap.get

get(name):

Fetches an object whose .conn attribute represents a connection to an HTTP server invoked through SOAP.

Parameters:name (string) – Name of the SOAP connection template
Return type:(A thin wrapper around a connection to the server)

Note

Regardless of whether the connection is to a plain HTTP server or to a SOAP one, the object returned offers a .conn attribute over which servers are actually accessed using methods and attributes listed below.

Name Type Notes
auth attribute A username/password configured for the given HTTP connection, if any has been configured at all
ping method Pings the remote server
get method Issues a GET request
post method Issues a POST request
send method Alias to .post
session attribute A handle to the underlying Requests library which can be used to issue any other requests possible
ping(cid):

Uses HTTP HEAD to chech whether the remote server is alive, returns a text report on how long it took for the other end to reply.

Parameters:cid (string) – A correlation ID that can be used to establish a connection between the Zato request and any entries in the logs the other end may produce
Return type:(A text report regarding how long it took for the other side to reply)
get(cid, params=None, prefetch=True, *args, **kwargs):

Issues an HTTP GET request.

Parameters:
  • cid (string) – A correlation ID that can be used to establish a connection between the Zato request and any entries in the logs the other end may produce
  • params (dict) – A dictionary of GET parameters to use
  • prefetch (bool) – If the response should be downloaded immediately
  • args (tuple) – Positional arguments passed to the underlying HTTP library
  • kwargs (dict) – Keyword arguments passed to the underlying HTTP library
Return type:

An instance of requests.Response class

.post

post(cid, data='', prefetch=True, *args, **kwargs):

Issues an HTTP POST request.

Parameters:
  • cid (string) – A correlation ID that can be used to establish a connection between the Zato request and any entries in the logs the other end may produce
  • data – String data to send to the remote peer
  • prefetch (bool) – If the response should be downloaded immediately
  • args (tuple) – Positional arguments passed to the underlying HTTP library
  • kwargs (dict) – Keyword arguments passed to the underlying HTTP library
Return type:

An instance of requests.Response class

Usage examples

Plain HTTP - POST JSON

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# stdlib
from json import dumps

# Zato
from zato.server.service import Service

class MyService(Service):
    def handle(self):

        cust_profile = self.outgoing.plain_http.get('CRM Customer Profile')
        cust_cases = self.outgoing.plain_http.get('CRM Customer Cases')

        request = dumps({'cust_id':1, 'name':'Foo Bar'})
        response = cust_profile.conn.post(self.cid, request)

        self.logger.info(response.headers['content-type'])
        self.logger.info(response.text)

        ping_info = cust_cases.conn.ping(self.cid)
        self.logger.info(ping_info)

Plain HTTP - GET requests

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

class MyService(Service):
    def handle(self):
        out = self.outgoing.plain_http.get('CRM Connection')
        url_params = {'id':1, 'type_id':2}
        response = out.conn.get(self.cid, url_params)
        self.logger.info(response.text)

SOAP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# lxml
from lxml import etree

# Zato
from zato.server.service import Service

class MyService(Service):
    def handle(self):
        service = self.outgoing.soap.get('Set Account Balance')
        resp = service.conn.send(self.cid, '<data><id>123</id><val>45.67</val></data>')

        # Parse resp.text to create an XML object
        xml = etree.fromstring(resp.text)