# Need help using URI module for a curl command with json body

**URL:** <https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765>\
**Category:** Ansible Project\
**Created:** [December 4, 2019, 10:41pm UTC](https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765 "2019-12-04T22:41:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Heppe](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Robert\_Heppe](https://forum.ansible.com/u/Robert_Heppe)\
**Post date:** [December 4, 2019, 10:41pm UTC](https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765/1 "2019-12-04T22:41:41Z")

</div>

Hi all,  
I have a working curl command that I’ve been trying to get into an ansible playbook in a testing environment. I’ve found several posts on various forums for similar situations, but not the one I’m working with, so my attempts are still erroring. I have the curl below, which when run on the cli works correctly.

curl -X POST -k -u xxxx:xxxx “[https://x.x.x.x:5601/api/spaces/space](https://x.x.x.x:5601/api/spaces/space)” -H ‘kbn-xsrf: true’ -H ‘Content-Type: application/json’ -d’  
{  
“id”: “some\_id”,  
“name”: “some\_name”,  
“disabledFeatures”: [“visualize”,“dev\_tools”,“advancedSettings”,“indexPatterns”,“savedObjectsManagement”,“timelion”,“graph”,“monitoring”,“ml”,“apm”,“maps”,“canvas”,“infrastructure”,“logs”,“siem”,“uptime”]  
}  
’

However, I have tried many variations of it in a playbook without success. Can anyone suggestions modifications to this playbook?

---

<div class="post-metadata">

**Author:** ![nschende](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nschende/32/1681_2.png) [@nschende](https://forum.ansible.com/u/nschende)\
**Post date:** [December 4, 2019, 11:14pm UTC](https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765/2 "2019-12-04T23:14:18Z")

</div>

Have you tried the body section with the the curly braces? Like so:

body:

“id”: “{{ customer\_id }}”,  
“name”: “{{ customer\_name }}”,  
“disabledFeatures”: [“visualize”,“dev\_tools”,“advancedSettings”,“indexPatterns”,“savedObjectsManagement”,“timelion”,“graph”,“monitoring”,“ml”,“apm”,“maps”,“canvas”,“infrastructure”,“logs”,“siem”,“uptime”]

I have a playbook using URI module with a json body and mine works without those.

---

<div class="post-metadata">

**Author:** ![Robert\_Heppe](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Robert\_Heppe](https://forum.ansible.com/u/Robert_Heppe)\
**Post date:** [December 5, 2019, 12:24am UTC](https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765/3 "2019-12-05T00:24:51Z")

</div>

Thanks Nick,

I have not. I just tried this but get another error for the , at the end of the line. So I removed them and it errors again.

body:  
“id”: “{{ customer\_id }}”  
“name”: “{{ customer\_name }}”  
“disabledFeatures”: [“visualize”,“dev\_tools”,“advancedSettings”,“indexPatterns”,“savedObjectsManagement”,“timelion”,“graph”,“monitoring”,“ml”,“apm”,“maps”,“canvas”,“infrastructure”,“logs”,“siem”,“uptime”]

Could you post your entire uri block as an example and I could try to modify mine to match the syntax?

---

<div class="post-metadata">

**Author:** ![Bram\_Mertens](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@Bram\_Mertens](https://forum.ansible.com/u/Bram_Mertens)\
**Post date:** [December 5, 2019, 6:53am UTC](https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765/4 "2019-12-05T06:53:43Z")

</div>

Hi,

Perhaps it is only a problem in your example but in the example you appear to use an IP address for by a port. (x.x.x.x:5601) but in the playbook you seem to use a hostname.

Could it be a name resolution issue?  
Does it work if you hardcode the exact same URL in the playbook?

Another thing I noticed is that the URL in the playbook has a suffix of -ki01 (url: “https://{{ region }}-ki01:5601/api/spaces/space”).  
Is that intentional?

Hth

Bram

---

<div class="post-metadata">

**Author:** ![Robert\_Heppe](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Robert\_Heppe](https://forum.ansible.com/u/Robert_Heppe)\
**Post date:** [December 5, 2019, 6:23pm UTC](https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765/5 "2019-12-05T18:23:26Z")

</div>

Hi Bram,  
Yes, I just removed the IP for the post. I think connectivity should be fine as I can telnet to the hostname from the ansible system.

[centos@jumphost ~]$ telnet us1-ki01 5601  
Trying x.x.101.185…  
Connected to us1-ki01.  
Escape character is ‘^]’.  
^]

I wonder if I have the double headers entered correctly?

headers:  
kbn-xsrf: “true”  
Content-Type: “application/json”

Thanks,  
Robert

---

<div class="post-metadata">

**Author:** ![nschende](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nschende/32/1681_2.png) [@nschende](https://forum.ansible.com/u/nschende)\
**Post date:** [December 7, 2019, 12:39am UTC](https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765/6 "2019-12-07T00:39:01Z")

</div>

Sure, I will grab my entire example task in just a bit here. Happy to share if it might help.

---

<div class="post-metadata">

**Author:** ![nschende](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nschende/32/1681_2.png) [@nschende](https://forum.ansible.com/u/nschende)\
**Post date:** [December 12, 2019, 1:24am UTC](https://forum.ansible.com/t/need-help-using-uri-module-for-a-curl-command-with-json-body/31765/7 "2019-12-12T01:24:56Z")

</div>

Sorry for the delay, I meant to grab the example when I got back to my desk and lost track of it.  
Here is an example I am using currently that works

- name: restore disk from snapshot  
uri:  
url: https://{{ cluser\_ip }}/api/internal/vmware/vm/snapshot/{{snapshotlist.json.data[0].id}}/mount\_disks  
headers:  
Content-Type: “application/json”  
Authorization: “Bearer {{ authtoken.json.token }}” # pass authentication token from earlier rather than basic auth  
body:  
targetVmId: ‘{{ target\_vm\_id }}’ # VM to restore the disk snapshots to  
vmdkIds: [‘{{ snapshotdetails.json.snapshotDiskDetails[1].virtualDiskId }}’] # Rubrik wants an array of vmdk ID’s to restore, without it will restore all in the snapshot  
body\_format: json  
method: POST  
status\_code: 202 # Rubrik returns 202 on sucess.  
return\_content: yes  
validate\_certs: yes  
tags: restore  
delegate\_to: localhost  
become: no
