Skip to content

Commit

Permalink
Merge pull request mqtt-tools#158 from seblucas/master
Browse files Browse the repository at this point in the history
Added a fifth parameter to the HTTP service to send pure json
  • Loading branch information
jpmens committed Dec 2, 2015
2 parents ae1f429 + eae7865 commit d45f054
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
}
```
Expand Down
13 changes: 11 additions & 2 deletions services/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d45f054

Please sign in to comment.