# Alternative for uri module for multipart request on windows

**URL:** <https://forum.ansible.com/t/alternative-for-uri-module-for-multipart-request-on-windows/42515>\
**Category:** Get Help\
**Tags:** windows, ansible-project\
**Created:** [May 7, 2025, 12:18pm UTC](https://forum.ansible.com/t/alternative-for-uri-module-for-multipart-request-on-windows/42515 "2025-05-07T12:18:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkastelijns](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mkastelijns](https://forum.ansible.com/u/mkastelijns)\
**Post date:** [May 7, 2025, 12:18pm UTC](https://forum.ansible.com/t/alternative-for-uri-module-for-multipart-request-on-windows/42515/1 "2025-05-07T12:18:49Z")

</div>

Hello ansible community,

We are working with an Ansible project in which we would like to call a API-put request for FME (a GIS-etl-tool). The machine that gets the request is a windows machine. Therefore we quickly found the win\_uri module. However we do a multipart request in which a file is uploaded to the application. This would be easier if it were a linux-machine. then we could use the uri module. The win\_uri module does not support form-multipart as a variable in the body.

After some struggles the only solution that works so far is calling a python script deducted from postman, making the original API request.  
-The python-script is created based on the environment variables,  
-The python-script is run by ansible  
-The output is tested to see if it was succesful

I are curious if there is an easier way to achieve the same result. The code we have now is:

```auto
---
- name: Write Python script
  win_copy:
    content: "{{ python_script_content }}"
    dest: "{{ destination_path }}/script.py"
    force: yes

- name: Run the Python script
  win_shell: |
    python "{{ destination_path }}/script.py"
  register: upload_result

- name: Check upload success status
  debug:
    msg: "Upload succeeded with response code 204!"
  when: upload_result.rc == 0

- name: Upload failed or unexpected response
  debug:
    msg: "Upload failed or returned status code other than 204. Return code: {{ upload_result.rc }}"
  when: upload_result.rc != 0

```

and in the main the python script that is written to the machine and run via ansible:

```auto
python_script_content: |
  import requests
  import base64

  # Your credentials
  credentials = "{{ user }}:{{ password }}"

  # Encode credentials in base64
  b64_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')

  # Set headers with the encoded token
  headers = {
      'Authorization': f'Basic {b64_credentials}'
  }

  url = "https://{{ fqdn }}/fmeapiv4/configuration/encryption/keyfile"
  payload = {}
  file_path = "{{ share[env] }}/key/fme_{{ env }}.jceks"

  with open(file_path, 'rb') as f:
      files = [('file', ('fme_{{ env }}.jceks', f, 'application/octet-stream'))]
      response = requests.put(url, headers=headers, data=payload, files=files, verify=False)

  print(response.text)

```

Other options we tested but not the optimal solution or working at all:  
-Powershell script run via Ansible with win\_shell/ win\_script  
-Use CURL in some way  
-Somehow use the win\_uri incorporating multipart as header

Is there an option to achieve the same goal more easily?

---

<div class="post-metadata">

**Author:** ![wayt](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@wayt](https://forum.ansible.com/u/wayt)\
**Post date:** [May 9, 2025, 4:08pm UTC](https://forum.ansible.com/t/alternative-for-uri-module-for-multipart-request-on-windows/42515/2 "2025-05-09T16:08:14Z")

</div>

Could you fetch the file to the control node and use the more capable Linux based uri module available there?

I’d recommend creating a temporary file for each host in the play loop delegated to localhost (control node) and keeping that in a variable via register to avoid conflicts due to parallelism.
