# Put request to update only one attribute from API GET

**URL:** <https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611>\
**Category:** Ansible Project\
**Created:** [June 20, 2019, 6:28am UTC](https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611 "2019-06-20T06:28:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [June 20, 2019, 6:28am UTC](https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611/1 "2019-06-20T06:28:04Z")

</div>

Hello,

I’m trying to update an attribute from a GET API JSON result and do a PUT request along with the entire API data with only one attribute change. Please advice on the format for PUT Req Task ?

1. Get Request for a given API call
2. From the above json data in step1, I need to update only one attribute: mipUser (From "false To "true) and do a PUT request along with the other data unchanged in the json.

`

- name: GET Request for CLUSTERS  
uri:  
url: [https://zsapi.com/BETA/](https://zsapi.com/BETA/){{ item }}  
method: GET  
validate\_certs: no  
return\_content: yes  
status\_code: 200  
headers:  
Content-Type: application/json  
Cookie: “{{ login.set\_cookie.split(‘;’)[0] }}”  
body\_format: json  
with\_items: “{{ cluster }}”  
register: cluster\_result

`

`

ok: [localhost] =\> {  
“msg”: [  
{  
“bandwidthControlApp”: true,  
“cloud”: “BETA”,  
“createTime”: 1550875210,  
“datacenter”: {  
“id”: 526,  
“name”: “FMT1”  
},  
“description”: “cluster with VIP 169.254.1.100”,  
“disabled”: false,  
“id”: 2334,  
“lastModifiedTime”: 1560896398,  
“lastModifiedUser”: {  
“id”: 1009,  
“name”: “[adev@xyz.com](mailto:adev@xyz.com)”  
},  
“mipUser”: false,  
“mtsInstances”: ,  
“name”: “fmt1”,  
“rateLimit”: true,

}  
]  
}

`

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 20, 2019, 7:12am UTC](https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611/2 "2019-06-20T07:12:45Z")

</div>

The combine filter should allow you to do this:

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#combining-hashes-dictionaries](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#combining-hashes-dictionaries)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 20, 2019, 11:09am UTC](https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611/3 "2019-06-20T11:09:52Z")

</div>

So you can use something like this for the PUT request:

"{{ firstresult | combine({'mipUser': true}) }}"

---

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [June 20, 2019, 5:12pm UTC](https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611/4 "2019-06-20T17:12:47Z")

</div>

Hi Dick,

For this to work, I need to convert the output to a dictionary. Hw can I do that ?

fatal: [localhost]: FAILED! =\> {“msg”: "|combine expects dictionaries, got [{u’bandwidthControlApp’: F

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 21, 2019, 4:09am UTC](https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611/5 "2019-06-21T04:09:23Z")

</div>

You need to use that in a loop as the results variable is a list of dicts.

Or drop the loop if you only use it once so the result will be a dict.

Dick

---

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [June 24, 2019, 7:03am UTC](https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611/6 "2019-06-24T07:03:44Z")

</div>

Hi Dick,

I was able to use combine filter and save the updated json content to a variable using set\_fact. Now, when I try to do PUT request using this variable in the body I get error. Can you help me with the body syntax using a set\_fact variable ?

`

- set\_fact:  
updated\_flag: “{{ cluster\_result | combine({‘traceSyncApp’: true}) }}”

- debug:  
msg: “{{ updated\_flag }}”

- name: Enable Tracesync  
uri:  
url:   
method: PUT  
validate\_certs: no  
return\_content: yes  
status\_code: 200  
headers:  
Content-Type: application/json  
Cookie: “{{ login.set\_cookie.split(‘;’)[0] }}”  
body\_format: json  
body : “{{ updated\_flag }}”  
register: cluster\_result  
`

`  
fatal: [localhost]: FAILED! =\> {“changed”: false, “connection”: “close”, “content”: “Request body is invalid.”, “content\_length”: “24”, “content\_type”: “text/plain”, “date”: “Mon, 24 Jun 2019 06:58:51 GMT”, “failed”: true, “msg”: "Status code was not [200]: HTTP Error 400: "}

`

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 24, 2019, 10:06am UTC](https://forum.ansible.com/t/put-request-to-update-only-one-attribute-from-api-get/30611/7 "2019-06-24T10:06:49Z")

</div>

That error is specific to the API you're calling.  
To debug, compare and look for difference between the data that you  
send in this request, and the data in a task that does succeed.

Dick
