Search code examples
jsonansiblegraylog

Why does the Ansible uri module get 400 with valid credenitals?


We are using the uri module for querying the Graylog API the data, which is in json format.

- name: Get data
  uri:
    url: http://graylog-host:9000/api/system
    url_username: username
    url_password: password
    force_basic_auth: yes
    return_content: yes
    method: GET
    body_format: json
    validate_certs: false
  register: node_id_output

Unfortunately, with this task we get following error:

fatal: [hostname]: FAILED! => {"changed": false, "connection": "close", "content": "", "content_length": "0", "date": "Wed, 02 Sep 2020 07:21:25 GMT", "elapsed": 0, "msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request", "redirected": false, "status": 400, "url": "http://graylog-host:9000/api/system"}

Although following Curl command works fine and returns valid JSON content with status code 200:

curl -u username:password -H "Accept: application/json"  http://graylog-host:9000/api/system

What would be the cause of the error? Alternatively we can use body_format: form-urlencoded, which does not return 400, but unfortunately then the content is not parsable by json_query filter.


Solution

  • Using body_format: json will set the header Content-Type: application/json and send the value of the body parameter which will be null.

    In your case, you want to set the header Accept: application/json to get JSON back instead of something else (XML I guess).

    - name: Get data
      uri:
        url: http://graylog-host:9000/api/system
        url_username: username
        url_password: password
        force_basic_auth: yes
        return_content: yes
        method: GET
        headers:
          Accept: application/json
        validate_certs: false
      register: node_id_output