# Issues with connecting to API using Ansible

**URL:** <https://forum.ansible.com/t/issues-with-connecting-to-api-using-ansible/29353>\
**Category:** Ansible Project\
**Created:** [January 2, 2019, 4:41pm UTC](https://forum.ansible.com/t/issues-with-connecting-to-api-using-ansible/29353 "2019-01-02T16:41:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hristo\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@Hristo\_Georgiev](https://forum.ansible.com/u/Hristo_Georgiev)\
**Post date:** [January 2, 2019, 4:41pm UTC](https://forum.ansible.com/t/issues-with-connecting-to-api-using-ansible/29353/1 "2019-01-02T16:41:05Z")

</div>

Hello,

I have a WebService that I need to connect to and add content via POST method. It accepts content in the form of “application/x-www-form-urlencoded”. I am using JSON formatted body and I tell Ansible to use form-urlencoded body format. I do not get any error, the response code is 200, but the content does not get posted to the WebService. Meanwhile, I use the exact same JSON file in a Postman software, again I set it to use form-urlencoded format. and the Postman posts the content to the WebService fine. I am missing something in Ansible and I cannot figure out what. Can you please help?

[root@ansible ~]# python --version  
Python 2.7.5  
[root@ansible ~]#

[root@ansible ~]# ansible --version --quiet  
ansible 2.7.5

**Here is my play:**

- name: Sample Task  
local\_action:  
module: uri  
url:   
method: POST

headers:  
Content-Type: “application/x-www-form-urlencoded”  
body\_format: json  
body: “{{ lookup(‘file’,‘/tmp/outage.json’) }}”  
use\_proxy: no  
validate\_certs: no  
timeout: 300

**Here is my JSON file:**

param={  
“Username”: “_**",  
“Password”: "**_\*”,  
“MessageInfo”: {  
“messageId”: “”,  
“messageType”: “Some Type”,  
“category”: “Some Category”,  
“message”: “Message to be added to the WebService”,  
“recurrenceFlag”: “0”,  
“indCode”: “”,  
“requestor”: “”,  
“implementer”: “”,  
“ticketNumber”: “Ticke tnumber”,  
“systemList”: [  
{  
“systemId”: “”,  
“systemName”: “The system the message should be added to”,  
“ucmdbCiId”: “”  
}  
],  
“instanceList”: ,  
“schedule”: {  
“tzName”: “(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna”,  
“startDate”: “25.03.2019 00:30”,  
“endDate”: “25.03.2019 04:15”,  
“startDay”: null,  
“endDay”: null,  
“startTime”: null,  
“duration”: null,  
“weeklyDays”: null,  
“monthlyDay”: null  
}  
}  
}

**Here is the Ansible output:**

TASK [pre\_patching : Sample Task] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
.  
.  
ok: [127.0.0.5 → localhost] =\> {  
“access\_control\_allow\_headers”: “Origin, X-Requested-With, Content-Type, Accept, LastModified, Authorization”,  
“access\_control\_allow\_methods”: “GET, POST, HEAD, PUT, DELETE”,  
“access\_control\_allow\_origin”: “\*”,  
“access\_control\_max\_age”: “3600”,  
“changed”: false,  
“connection”: “close”,  
“content\_length”: “103”,  
“content\_type”: “application/json;charset=utf-8”,  
“cookies”: {  
“JSESSIONID”: “6704FFEF9C430A10794FB83A0B9F4533”  
},  
“cookies\_string”: “JSESSIONID=6704FFEF9C430A10794FB83A0B9F4533”,  
“date”: “Wed, 02 Jan 2019 15:29:08 GMT”,  
“invocation”: {  
“module\_args”: {  
“attributes”: null,  
“backup”: null,  
“body”: “param={\n "Username": "_ **",\n "Password": "** _",\n "MessageInfo": {\n "messageId": "",\n "messageType": "Some Type",\ "category": "Some Category",\n "message": "Message to be added to the WebService",\n "recurrenceFlag": "0",\n "indCode": "",\n "requestor": ",\n "implementer": "",\n "ticketNumber": "Ticke tnumber",\n "systemList": [\n {\n "systemId": "",\n "systemName": "The system the message should be added to",\n "ucmdbCiId": ""\n }\n],\n "instanceList": ,\n "schedule": {\n "tzName": "(GMT+01:00) Amsterdam, Berlin, Ber, Rome, Stockholm, Vienna",\n "startDate": "25.03.2019 00:30",\n "endDate": "25.03.2019 04:15",\n "startDay": null,\n "endDay": null,\n "startTime": null,\n "duration": null,\n "weeklyDays": null,\n "monthlyDay": null\n }\n }\n}”,  
“body\_format”: “json”,  
“client\_cert”: null,  
“client\_key”: null,  
“content”: null,  
“creates”: null,  
“delimiter”: null,  
“dest”: null,  
“directory\_mode”: null,  
“follow”: false,  
“follow\_redirects”: “safe”,  
“force”: false,  
“force\_basic\_auth”: false,  
“group”: null,  
“headers”: {  
“Content-Type”: “application/x-www-form-urlencoded”  
},  
“http\_agent”: “ansible-httpget”,  
“method”: “POST”,  
“mode”: null,  
“owner”: null,  
“regexp”: null,  
“remote\_src”: null,  
“removes”: null,  
“return\_content”: false,  
“selevel”: null,  
“serole”: null,  
“setype”: null,  
“seuser”: null,  
“src”: null,  
“status\_code”: [  
200  
],  
“timeout”: 300,  
“unsafe\_writes”: null,  
“url”: “”,  
“url\_password”: null,  
“url\_username”: null,  
“use\_proxy”: false,  
“validate\_certs”: false  
}  
},  
“msg”: “OK (103 bytes)”,  
“redirected”: false,  
“server”: “Apache-Coyote/1.1”,  
“set\_cookie”: “JSESSIONID=6704FFEF9C430A10794FB83A0B9F4533; Path=/esl/; Secure; HttpOnly”,  
“status”: 200,  
“url”: “”

---

<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:** [January 2, 2019, 4:48pm UTC](https://forum.ansible.com/t/issues-with-connecting-to-api-using-ansible/29353/2 "2019-01-02T16:48:51Z")

</div>

This is not valid json file, to make it valid "param=" must be removed.

---

<div class="post-metadata">

**Author:** ![Hristo\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@Hristo\_Georgiev](https://forum.ansible.com/u/Hristo_Georgiev)\
**Post date:** [January 2, 2019, 5:02pm UTC](https://forum.ansible.com/t/issues-with-connecting-to-api-using-ansible/29353/3 "2019-01-02T17:02:20Z")

</div>

Hi, good point, I removed the “param=” part and the result is still the same. I added the “param=” part because the WebService is using it. For instance, when I use the same JSON file in the Postmane software, I do not add “param=” to it, instead I specify it as a key. The software has the fields “key” and “value”, I put “param=” as a key and the JSON content as a “value”.  
How can I do that in Ansible, how to specify the key?

---

<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:** [January 2, 2019, 5:20pm UTC](https://forum.ansible.com/t/issues-with-connecting-to-api-using-ansible/29353/4 "2019-01-02T17:20:46Z")

</div>

If you set  
&nbsp;&nbsp;body\_format: form-urlencoded

and then body to

&nbsp;&nbsp;body:  
&nbsp;&nbsp;&nbsp;&nbsp;param: "{{ lookup('file','/tmp/outage.json') }}"

and remove the headers: you will in the body get "parm={......" with proper urlencoding.

---

<div class="post-metadata">

**Author:** ![Hristo\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@Hristo\_Georgiev](https://forum.ansible.com/u/Hristo_Georgiev)\
**Post date:** [January 2, 2019, 7:55pm UTC](https://forum.ansible.com/t/issues-with-connecting-to-api-using-ansible/29353/5 "2019-01-02T19:55:13Z")

</div>

Thanks, that sounds logical, and there seems to be progress, the error now is different, I think there is something small that needs to be adjusted

“content”: “\<ns1:XMLFault xmlns:ns1="[http://cxf.apache.org/bindings/xformat\](http://cxf.apache.org/bindings/xformat%5C)”\>\<ns1:faultstring xmlns:ns1="[http://cxf.apache.org/bindings/xformat\](http://cxf.apache.org/bindings/xformat%5C)“\>org.codehaus.jettison.json.JSONException: A JSONObject text must begin with ‘{’ at character 1 of Username&param=MessageInfo&param=Password\</ns1:faultstring\>\</ns1:XMLFault\>”,
