uri module change

Hi guys,

I’m after a bit of feedback on the uri module.

The doc has the following example:

- uri: url=https://your.jira.example.com/rest/api/2/issue/
       method=POST user=your_username password=your_pass
       body="{{ lookup('file','issue.json') }}" force_basic_auth=yes
       status_code=201 HEADER_Content-Type="application/json"
All good.  However, if you write pure json in the file 'issue.json' you will get the following problem when you run the module:

Traceback (most recent call last):
File “/root/.ansible/tmp/ansible-tmp-1431569317.82-251864554272416/uri”, line 2049, in
main()
File “/root/.ansible/tmp/ansible-tmp-1431569317.82-251864554272416/uri”, line 407, in main
resp, content, dest = uri(module, url, dest, user, password, body, method, dict_headers, redirects, socket_timeout)
File “/root/.ansible/tmp/ansible-tmp-1431569317.82-251864554272416/uri”, line 306, in uri
resp, content = h.request(url, method=method, body=body, headers=headers)
File “/usr/lib/python2.6/site-packages/httplib2/init.py”, line 1605, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File “/usr/lib/python2.6/site-packages/httplib2/init.py”, line 1353, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File “/usr/lib/python2.6/site-packages/httplib2/init.py”, line 1290, in _conn_request
conn.request(method, request_uri, body, headers)
File “/usr/lib64/python2.6/httplib.py”, line 920, in request
self._send_request(method, url, body, headers)
File “/usr/lib64/python2.6/httplib.py”, line 960, in _send_request
self.send(body)
File “/usr/lib64/python2.6/httplib.py”, line 765, in send
self.sock.sendall(str)
File “”, line 1, in sendall
TypeError: sendall() argument 1 must be string or buffer, not dict

The json file gets interpreted as a Python dict and httplib.py errors out when it’s passed a dict instead of a string.

My solution was to do this before the request (line 302 in uri module):

if isinstance(body, dict):
body = json.dumps(body)

I just wanted to know if this is worth a PR or would a better solution be to stop the file being interpreted as a dict in the first place?

Hi Rob,
I ran into this problem using the uri module when I started adding variables into the body. The only solution I could get to work was the change you suggested. I think it would definitely be worth a PR.
Thanks,
Beatrice.

  • local_action:
    module: uri
    url: http://XXXXX:4567/stashes
    method: POST
    status_code: 201
    HEADER_Content-Type: application/json
    body: ‘{“content”:{“message”:“temp”}, “path”:“silence/a/{{domain}}”}’

If you try running the uri module that's in the devel branch,
https://github.com/ansible/ansible-modules-core/blob/devel/network/basics/uri.py
are you able to use the body_format=json parameter to make this work?

The module probably still needs a PR to update the documentation,
though. If you could confirm that the new parameter is working and
then submit a PR to update the documentation that would be great!

-Toshio

I’ll test this next week and submit doc PR

Tested and working. Thanks for the work Toshio. PR for doc update - https://github.com/ansible/ansible-modules-core/pull/1430