Sending JSON variable as a body in curl command

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”]}

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”, “-k”, “-d”, “{ustatus:”, “ucomplete,”, “umessage:”, “usuccessfully completed,”, “uactions:”, “[uclose ticket]}”,

Any resolution?

Thank you in advance.

Bikram

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/\{{ 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", "-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.

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:

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

                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/\{{ 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", "-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

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

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.

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

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/{{ ticket }} -k --data ‘{“status”:“complete”,“message”:“unsuccessfully completed”,“actions”:[“close ticket”]}’ --header “{{ access_token }}”

Thank you very much.

Bikram

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