But when I try to download the same attachment, either the CSV is garbled or encrypted or PDF says, cannot be loaded. Please see the code and the error below.
Ansible code to upload a CSV or PDF file
- name: "Attach output CSV file to incident"
ansible.builtin.uri:
url: "{{ SN_HOST }}api/now/attachment/upload"
user: "{{ SN_USERNAME }}"
password: "{{ SN_PASSWORD }}"
method: POST
return_content: true
validate_certs: false
force_basic_auth: no
status_code: 201
body_format: form-multipart
headers:
Accept: 'application/json'
Content-Type: 'multipart/form-data'
body:
table_name:
content: incident
table_sys_id:
content: 'c6957640470388444c736d4315'
uploadFile:
filename: "../../win_playbooks/reports/User_Manual.pdf"
register: attach_file
Error when trying to open the attached PDF file
Error when trying to open the downloaded attached CSV file
When I use the ServiceNow REST API explorer or the ServiceNow UI manually to upload and download the attached PDF or CSV file, it works perfectly. I guess the issue is that, when I try to upload the PDF or CSV from the above mentioned Ansible code, it is not working.
As a temporary fix, I directly used
curl
command inside an Ansible task which does not encode any file when attached to a ServiceNow Incident Record.I believe the behavior of the Ansible
uri
module which when used withbody_format: form-multipart
will auto encode with base64 (charset=UTF-8) and at present, it seems there is no way to bypass encoding with this body format.So
curl
is the only option for now.