'Ansible win_uri module doesn't return content for POST query

I try to get some data from the page after the POST query. I'm using ansible 2.9.10. But the win_uri module in it doesn't return the content of the response. But Invoke-WebRequest working on the same host.

  win_uri:
    method: POST
    url: "{{ configuraton_url }}"
    body: "{'action': 'listconfig'}"
    validate_certs: no
    use_default_credential: yes
    return_content: yes
  register: configuration

result

  configuration:
    cache_control: private
    changed: false
    character_set: null
    content: ''
    content_encoding: ''
    content_length: '0'
    content_type: ''
    cookies: []
    date: Mon, 29 Jun 2020 20:17:29 GMT
    elapsed: 0.1875025
    failed: false
    headers:
    - Persistent-Auth
    - Content-Length
    - Cache-Control
    - Date
    - Server
    - WWW-Authenticate
    - X-AspNet-Version
    - X-Powered-By
    is_from_cache: false
    is_mutually_authenticated: true
    last_modified: '2020-06-29T20:17:29.5951209+00:00'
    method: POST
    msg: OK
    persistent_auth: 'true'
    protocol_version:
      Build: -1
      Major: 1
      MajorRevision: -1
      Minor: 1
      MinorRevision: -1
      Revision: -1
    response_uri: https://xxxxxxx:xxx/xxxxxxxx
    server: xxxxxxx
    status_code: 200
    status_description: OK
    supports_headers: true
    url: https://xxxxxxx:xxx/xxxxxxxx
    www_authenticate: Negotiate oRswGaADCgEAoxIEEAEAAAAyRzN6JblgagAAAAA=
    x_asp_net_version: xxxxx
    x_powered_by: xxxxx


Solution 1:[1]

content_length: '0'

It's doing exactly as the server instructed, there is no content returned

No one on Stack Overlow will be able to tell you why your obfuscated server did not return any content, but ansible is doing as it was instructed


As a guess, I would guess you are sending a malformed payload, both that body: should have the quotes reversed (since the single-quote version is legal python, but not legal JSON) and you are missing the content_type: application/json in order to tell win_uri: not to use x-www-form-urlencoded. We can't know for sure that's what's happening because you removed the rest of the module output

  win_uri:
    method: POST
    url: "{{ configuraton_url }}"
    body: '{"action": "listconfig"}'
    content_type: application/json

Solution 2:[2]

I had this same issue. But found this article: https://github.com/ansible/ansible/issues/34698 and discovered that I had not used the return_content parameter. The default is to NOT return the content.

https://docs.ansible.com/ansible/2.9/modules/win_uri_module.html#win-uri-module

After adding "return_content: yes" the task was working as expected.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mdaniel
Solution 2 Shawn