URI Multipart for Image file

Hello All,

I am trying to upload the image using below URI API call with multipart/form-data as i have almost 1 to 6 image to be uploaded at a time.

For testing i am only trying with the image 1.

Task work as expected but the issue is upload image are all scattered. I tried to upload using the curl and with GUI work perfectly.

Can anyone help.

  • name: multipart api call
    uri:
    url: “IP/api/xxxx/images”
    method: POST
    headers:
    Content-Type: “multipart/form-data”
    Authorization: “Bearer {{ access_token }}”
    validate_certs: <true/false>
    body_format: form-multipart
    body:
    image1:
    content: “{{ lookup(‘file’, ‘path_to_image.jpg’)}}”
    filename: “path_to_image.jpg”
    type: image/jpg
    register: register_uri_val

delegate_to: localhost

You cannot use lookup to get the image, because the templating engine will attempt to convert everything to utf-8 text within the data.

Just use the path to the image in the filename, and drop content

Hi Matt,

Thank you for the revert. I have tried with that approach already as below but then i get the error pasted below. Only way pass through for me was with the content but image pixel are all blown out

body:
image1:
filename: “path_to_image.jpg”
mime_type/type: image/jpg
register: register_uri_val

delegate_to: localhost

Error
fatal: [IP → localhost]: FAILED! => {“cache_control”: “no-cache, no-store”, “changed”: false, “connection”: “close”, “content_security_policy”: “default-src ‘self’ ‘unsafe-inline’ https://www.gstatic.com *.githubusercontent.com data: blob: ws: wss:; script-src ‘self’ ‘unsafe-inline’ https://www.gstatic.com”, “content_type”: “application/json;charset=UTF-8”, “date”: “Fri, 24 Sep 2021 13:53:46 GMT”, “elapsed”: 0, “expires”: “-1”, “feature_policy”: “vibrate ‘none’ ; microphone * ; camera ‘none’ ; gyroscope ‘none’ ; magnetometer ‘none’ ; geolocation ‘none’ ; midi ‘self’ ; notifications ‘self’ ; push ‘self’ ; sync-xhr ‘self’”, “json”: {“msg”: “Looks like the server threw a gasket”, “success”: false}, “msg”: "Status code was 500 and not [200]: HTTP Error 500: ", “pragma”: “no-cache”, “redirected”: false, “referrer_policy”: “same-origin”, “set_cookie”: “JSESSIONID=E8xxxxxxxxxxxxxxxxxxxxxA950E38; Path=/; Secure; HttpOnly”, “status”: 500, “strict_transport_security”: “max-age=31536000”, “transfer_encoding”: “chunked”, “url”: “https://192.x.x.x/api/settings/images”, “x_content_type_options”: “nosniff”, “x_frame_options”: “SAMEORIGIN”}

Haha, that error message from the server isn’t exactly helpful:

Looks like the server threw a gasket

I cannot really say without either seeing a working curl command, or knowing more about what caused the 500 ISE.

I do notice your latest task says “mime_type/type”, which should just be “mime_type”