3

I have a file attachment I'm trying to upload to Confluence via the REST API. I can do it by replicating their example with curl, but it's failing when using Ansible and I can't figure out why.

Here's the curl command:

curl -u <username>:<password> -X POST -H "X-Atlassian-Token: nocheck" -F "file=@<filepath>" -F "comment=File attached via REST API" https://myconfluenceserver.com/rest/api/content/<page ID>/child/attachment

Of course the values in <> are replaced with real values. This command is successful.

Here's the Ansible task:

ansible.builtin.uri:
  user: "{{ osa_credentials.username }}"
  password: "{{ osa_credentials.password }}"
  url: "{{ url }}"
  method: POST
  headers:
    X-Atlassian-Token: nocheck
  force_basic_auth: yes
  src: "{{ filepath }}"
  validate_certs: no

When I run the curl command it works, but with Ansible it throws error 400. Here is the full error text:

fatal: [localhost]: FAILED! => {
    "changed": false,
    "connection": "close",
    "content_language": "en",
    "content_length": "435",
    "content_type": "text/html;charset=utf-8",
    "date": "Thu, 29 Jun 2023 06:34:10 GMT",
    "elapsed": 1,
    "invocation": {
        "module_args": {
            "attributes": null,
            "body": null,
            "body_format": "raw",
            "ca_path": null,
            "ciphers": null,
            "client_cert": null,
            "client_key": null,
            "creates": null,
            "decompress": true,
            "dest": null,
            "follow_redirects": "safe",
            "force": false,
            "force_basic_auth": true,
            "group": null,
            "headers": {
                "Content-Length": 543818,
                "X-Atlassian-Token": "nocheck"
            },
            "http_agent": "ansible-httpget",
            "method": "POST",
            "mode": null,
            "owner": null,
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "remote_src": false,
            "removes": null,
            "return_content": false,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": "<filepath>",
            "status_code": [
                200
            ],
            "timeout": 30,
            "unix_socket": null,
            "unredirected_headers": [],
            "unsafe_writes": true,
            "url": "https://myconfluenceserver.com/rest/api/content/<page ID>/child/attachment",
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "url_username": "<username>",
            "use_gssapi": false,
            "use_netrc": true,
            "use_proxy": true,
            "user": "<username>",
            "validate_certs": false
        }
    },
    "msg": "Status code was 400 and not [200]: HTTP Error 400: ",
    "redirected": false,
    "server": "Apache",
    "status": 400,
    "url": "https://myconfluenceserver.com/rest/api/content/<page ID>/child/attachment"
}

What am I missing?

U880D
  • 8,601
  • 6
  • 24
  • 40
jeremywat
  • 181
  • 7
  • can you share the error reported by ansible ? in debug mod would be quite helpful ? – error404 Jun 29 '23 at 14:22
  • I updated the post with the full task result. The error message just says `Status code was 400 and not [200]: HTTP Error 400:` – jeremywat Jun 29 '23 at 17:40
  • Have you checked that the response file defined in `src: "{{ filepath }}"` contains all the API expects to get? Like the `file` form field with the file contents, and a `comment` field with the upload comment? Is there any useful logs on the Confluence server that might help debug what Ansible is missing? – Byob Jun 29 '23 at 20:10
  • can you access confluence logs? maybe it will tell you the reason of the error. you could also try to add the body_format parameter – The_Pingu Jun 30 '23 at 15:42
  • @Byob I'm not sure what you mean by "response file". `src: "{{ filepath }}"` is the file that I'm uploading as an attachment. It doesn't contain the `file`, `comment`, or any other fields. And unfortunately I don't have access to the confluence logs since I'm just a user on the confluence side – jeremywat Jun 30 '23 at 21:08

1 Answers1

2

Confluence REST API attachment - What content type to use? How to resolve HTTP Response Code 415?

According Ansible uri module with multipart/mixed? and the following minimal example

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    CONFLUENCE_USER: "user"
    CONFLUENCE_PASSWORD: "test"
    PAGE_ID: "123456789"

  tasks:

  - name: Test REST API - Confluence Attach File
    ansible.builtin.uri:
      user: "{{ CONFLUENCE_USER }}"
      password: "{{ CONFLUENCE_PASSWORD }}"
      url: "https://confluence.example.com/rest/api/content/{{ PAGE_ID }}/child/attachment"
      validate_certs: yes
      method: POST
      headers:
        X-Atlassian-Token: nocheck
      force_basic_auth: yes
      body_format: form-multipart
      body:
        file:
          content: "{{ lookup('file','test.file') }}"
          filename: test.file
    register: result

  - debug:
      var: result.json.results

a test run against Confluence will successful result into a file upload and output of

TASK [debug] *********
ok: [localhost] =>
  result.json.results:
  - _expandable:
      ancestors: ''
      body: ''
      children: /rest/api/content/987654321/child
      descendants: /rest/api/content/987654321/descendant
      history: /rest/api/content/987654321/history
      operations: ''
      restrictions: /rest/api/content/987654321/restriction/byOperation
      space: /rest/api/space/~user
    _links:
      download: /download/attachments/123456789/test.file?version=1&modificationDate=1688109306468&api=v2
      self: https://confluence.example.com/rest/api/content/987654321
      webui: /display/~user/StackOverflow?preview=%2F123456789%2F987654321%2Ftest.file
...
    status: current
    title: test.file
    type: attachment
...

So what's the right body format to use, or what's the right way to accomplish this?

Using

      body_format: form-multipart
      body:
        file:
          content: "{{ lookup('file','test.file') }}"
          filename: test.file

and as stated in man curl

-F, --form <name=content>

(HTTP SMTP IMAP) For HTTP protocol family, this lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388

Documentation

U880D
  • 8,601
  • 6
  • 24
  • 40