# Sending JSON variable as a body in curl command

**URL:** <https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896>\
**Category:** Ansible Project\
**Created:** [March 15, 2019, 10:55pm UTC](https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896 "2019-03-15T22:55:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bikram](https://avatars.discourse-cdn.com/v4/letter/b/f04885/32.png) [@Bikram](https://forum.ansible.com/u/Bikram)\
**Post date:** [March 15, 2019, 10:55pm UTC](https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896/1 "2019-03-15T22:55:50Z")

</div>

Hi All,

I defined a json variable in vars section and want to pass it as body in curl command as below:

vars:

- paydata: {“status”:“complete”,“message”:"successfully

completed",“actions”:[“close ticket”]}

- local\_action: command curl -X PUT -i -H “Content-Type:application/json” [https://ansibledev.corp.xxxx.com:8443/api/job/{{](https://ansibledev.corp.xxxx.com:8443/api/job/%7B%7B) ticket }} -k --data “{{ paydata }}” completed",“actions”:[“close ticket”]}’ --header

But here is the error I am getting. Seems “paydata” is not passed as expected.

fatal: [10.225.240.39 → localhost]: FAILED! =\> {“changed”: true, “cmd”: [“curl”, “-X”, “PUT”, “-i”, “-H”, “Content-Type:application/json”, “[https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22](https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22)”, “-k”, “-d”, “{ustatus:”, “ucomplete,”, “umessage:”, “usuccessfully completed,”, “uactions:”, “[uclose ticket]}”,

Any resolution?

Thank you in advance.

Bikram

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [March 16, 2019, 3:25am UTC](https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896/2 "2019-03-16T03:25:11Z")

</div>

> Hi All,
> 
> I defined a json variable in vars section and want to pass it as body in curl command as below:

Have you considered using the uri module instead?

> vars:
> 
> - paydata: {"status":"complete","message":"successfully
> 
> completed","actions":["close ticket"]}
> 
> - local\_action: command curl -X PUT -i -H "Content-Type:application/json" [https://ansibledev.corp.xxxx.com:8443/api/job/\](https://ansibledev.corp.xxxx.com:8443/api/job/%5C){{ ticket }} -k --data "{{ paydata }}" completed","actions":["close ticket"]}' --header
> 
> But here is the error I am getting. Seems "paydata" is not passed as expected.
> 
> fatal: [10.225.240.39 -\> localhost]: FAILED! =\> {"changed": true, "cmd": ["curl", "-X", "PUT", "-i", "-H", "Content-Type:application/json", "[https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22&quot;](https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22&quot;), "-k", "-d", "{ustatus:", "ucomplete,", "umessage:", "usuccessfully completed,", "uactions:", "[uclose ticket]}",
> 
> Any resolution?

Please post your playbook. It looks like the data needs proper escaping to be used in the command line.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 16, 2019, 6:21am UTC](https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896/3 "2019-03-16T06:21:17Z")

</div>

> I defined a json variable in vars section and want to pass it as body in  
> curl command as below:

As Sudheer mention you should take a look at the uri module.

> vars:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- paydata: {"status":"complete","message":"successfully
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;completed","actions":["close ticket"]}

This is not json in Ansible it's a dict.

> - local\_action: command curl -X PUT -i -H "Content-Type:application/json"  
> [https://ansibledev.corp.xxxx.com:8443/api/job/\](https://ansibledev.corp.xxxx.com:8443/api/job/%5C){{ ticket }} -k --data "{{  
> paydata }}" completed","actions":["close ticket"]}' --header
> 
> But here is the error I am getting. Seems "paydata" is not passed as  
> expected.
> 
> fatal: [10.225.240.39 -\> localhost]: FAILED! =\> {"changed": true, "cmd":  
> ["curl", "-X", "PUT", "-i", "-H", "Content-Type:application/json", "  
> [https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22&quot;](https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22&quot;), "-k",  
> "-d", "{ustatus:", "ucomplete,", "umessage:", "usuccessfully completed,",  
> "uactions:", "[uclose ticket]}",

To make a dict to be formated as json you need to use the to\_json filter  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#filters-for-formatting-data](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data)

---

<div class="post-metadata">

**Author:** ![Bikram](https://avatars.discourse-cdn.com/v4/letter/b/f04885/32.png) [@Bikram](https://forum.ansible.com/u/Bikram)\
**Post date:** [March 17, 2019, 4:13pm UTC](https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896/4 "2019-03-17T16:13:05Z")

</div>

Thanks for your feedback.

But there is a problem in ‘URI’ module.  
In “URI” module, there is no way to pass “access token”. Access token is required here as the url is OAUTH2.0 enabled.

You can get access token by passing client id and secret but you cannot pass it to uri module to get the response back. That’s why I am trying to use curl command and sending the JSON body.

Best Wishes,  
Bikram

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [March 18, 2019, 6:49am UTC](https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896/5 "2019-03-18T06:49:34Z")

</div>

Something seems off here.

I don't see how are you are passing 'access\_token' in your curl command line example.

You can send 'access\_token' via URI module in request body, header or URL.

---

<div class="post-metadata">

**Author:** ![Bikram](https://avatars.discourse-cdn.com/v4/letter/b/f04885/32.png) [@Bikram](https://forum.ansible.com/u/Bikram)\
**Post date:** [March 18, 2019, 6:48pm UTC](https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896/6 "2019-03-18T18:48:51Z")

</div>

Hi Sudheer,

Here is the format of my tasks and it is working perfectly with curl command but I want to send data from variable as I mentioned earlier.

- name: authenticate with restible via oauth2.0

uri:

body:

grant\_type: client\_credentials

body\_format: form-urlencoded

force\_basic\_auth: yes

method: POST

user: “{{ client\_id }}”

password: “{{ client\_secret}}”

status\_code: 201

url: [https://gns-ansible-dev.corp.xxx.com:8443/auth/token](https://gns-ansible-dev.corp.xxx.com:8443/auth/token)

validate\_certs: no

register: token\_auth\_response

- set\_fact: access\_token=“{{ ‘Authorization:’+‘Bearer’ + ’ ’ + token\_auth\_response.json.access\_token }}”

- local\_action: command curl -X PUT -i -H “Content-Type:application/json” [https://gns-ansible-dev.corp.xxx.com:8443/api/job/{{](https://gns-ansible-dev.corp.xxx.com:8443/api/job/%7B%7B) ticket }} -k --data ‘{“status”:“complete”,“message”:“unsuccessfully completed”,“actions”:[“close ticket”]}’ --header “{{ access\_token }}”

Thank you very much.

Bikram

---

<div class="post-metadata">

**Author:** ![Bikram](https://avatars.discourse-cdn.com/v4/letter/b/f04885/32.png) [@Bikram](https://forum.ansible.com/u/Bikram)\
**Post date:** [March 18, 2019, 9:42pm UTC](https://forum.ansible.com/t/sending-json-variable-as-a-body-in-curl-command/29896/7 "2019-03-18T21:42:09Z")

</div>

Hi Sudheer,

Sending access token in headers works.  
Here is the code section:

headers:

Authorization: “Bearer {{ token\_auth\_response.json.access\_token }}”

Content-Type: “application/json”

Thanks for your help.

Bikram
