Issues with connecting to API using Ansible

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”: “”

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

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?

If you set
  body_format: form-urlencoded

and then body to

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

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

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\”><ns1:faultstring xmlns:ns1="http://cxf.apache.org/bindings/xformat\“>org.codehaus.jettison.json.JSONException: A JSONObject text must begin with ‘{’ at character 1 of Username&param=MessageInfo&param=Password</ns1:faultstring></ns1:XMLFault>”,