diff --git a/README.md b/README.md index 401e8c38..cc7f7050 100644 --- a/README.md +++ b/README.md @@ -657,21 +657,22 @@ Requires: The `http` service allows GET and POST requests to an HTTP service. -Each target has four parameters: +Each target has five parameters: 1. The HTTP method (one of `get` or `post`) 2. The URL, which is transformed if possible (transformation errors are ignored) 3. `None` or a dict of parameters. Each individual parameter value is transformed. 4. `None` or a list of username/password e.g. `( 'username', 'password')` +5. `None` or True to force the transformation of the third parameter to a json object ```ini [config:http] timeout = 60 targets = { - #method #URL # query params or None # list auth + #method #URL # query params or None # list auth # Json 'get1' : [ "get", "http://example.org?", { 'q': '{name}', 'isod' : '{_dtiso}', 'xx': 'yy' }, ('username', 'password') ], - 'post1' : [ "post", "http://example.net", { 'q': '{name}', 'isod' : '{_dtiso}', 'xx': 'yy' }, None ] + 'post1' : [ "post", "http://example.net", { 'q': '{name}', 'isod' : '{_dtiso}', 'xx': 'yy' }, None, True ] } ``` diff --git a/services/http.py b/services/http.py index 5722d203..00e41e22 100644 --- a/services/http.py +++ b/services/http.py @@ -14,7 +14,7 @@ import simplejson as json def plugin(srv, item): - ''' addrs: (method, url dict(params), list(username, password)) ''' + ''' addrs: (method, url, dict(params), list(username, password), json) ''' srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target) @@ -30,6 +30,12 @@ def plugin(srv, item): except: pass + tojson = None + try: + tojson = item.addrs[4] + except: + pass + # Try and transform the URL. Use original URL if it's not possible try: url = url.format(**item.data) @@ -82,7 +88,10 @@ def plugin(srv, item): try: request = urllib2.Request(url) if params is not None: - encoded_params = urllib.urlencode(params) + if tojson is not None: + encoded_params = json.dumps(params) + else: + encoded_params = urllib.urlencode(params) else: encoded_params = message