# Post json data with URI module

**URL:** <https://forum.ansible.com/t/post-json-data-with-uri-module/17760>\
**Category:** Ansible Project\
**Created:** [March 17, 2015, 12:26am UTC](https://forum.ansible.com/t/post-json-data-with-uri-module/17760 "2015-03-17T00:26:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Kinsley](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@Tony\_Kinsley](https://forum.ansible.com/u/Tony_Kinsley)\
**Post date:** [March 17, 2015, 12:26am UTC](https://forum.ansible.com/t/post-json-data-with-uri-module/17760/1 "2015-03-17T00:26:34Z")

</div>

I am trying to post a variable that gets defined during my playbook as json to a webservice. Unfortunately I have not been able to get the URI module to post valid json.

`

- name: lookup act\_transaction in database  
mongo\_query: \>  
host={{ams\_ip}} user=user password={{pass}}  
database=db collection=transactions  
filter=‘{}’ projection=‘{“\_id”:True}’  
sort=‘start\_time’ direction=-1 limit=1  
register: query

- name: login to gui  
run\_once: yes  
uri:  
url: “https://{{ ip }}/login”  
method: POST  
follow\_redirects: all  
body: “username={{amg\_username}}&password={{amg\_password}}&next=/”  
HEADER\_Origin: “https://{{ ams\_ip }}”  
HEADER\_Content-Type: application/x-www-form-urlencoded  
register: cookie

- name: create request body  
set\_fact:  
req:  
\_id: “{{ query.results[0].\_id[‘$oid’] }}”  
guids: [“{{ guid }}”]

- debug: var=genconfig\_req

- debug: msg=“{{ genconfig\_req }}”

- name: generate config  
when: ams\_deploy and do\_act  
uri: \>  
url=“https://{{ ams\_ip }}/generate\_config”  
method=POST  
body=“{{ req|to\_nice\_json }}”  
follow\_redirects=all  
HEADER\_Origin=“https://{{ ams\_ip }}”  
HEADER\_Content-Type=application/json  
HEADER\_Cookie=“{{ cookie.set\_cookie }}”

`

when this request is made to the webserver, the webserver logs an error for invalid json ( SyntaxError: Unexpected token \_ ). I have tried every combination I can think of to escape our the dictionary as a valid json string. I added a debug failure log to the URI module to see what the URI module was seeing.:

`

if url == “[https://192.168.141.42/generate\_config](https://192.168.141.42/generate_config)”: # so we only fail on the problem task  
module.fail\_json(msg = “type = %s, body = %s” %( type( body ), str(body) ))  
resp, content = h.request(url, method=method, body=body, headers=headers)

`

This logs for me

`

failed: [192.168.141.120] =\> {“failed”: true}  
msg: type = \<type ‘str’\>, body = {\_id: 5504a68130600c6b05691b93, guids: [0021FA000000000002000000000007D1]}

`

which i think confirms that the module is just not sending valid json, the keys and values should be quoted. I assume the problem has something to do with the order the yaml is processed, the json filter gets applied before the playbook is analyzed, and ansible maybe sees the json data as more yaml so it gets loaded as such. I’m just guessing.

Interesting when I used the yaml format ( body: “{{ req }}” ) I was getting a dictionary instead of a string. I have tried decomposing the variable ( both yaml and inline format ) so:

`body='{ "_id": "{{ req._id }}", "guids": ["{{ guid }}"] }'`

I have tried the filters: string, to\_nice\_json, and to\_json

About the only thing I haven’t tried is writing the variable to a file using the copy module with content=“{{ req|to\_nice\_json }}” and then using a file lookup like the example shows.

Anybody have any ideas?
