URI Multipart for Image file

201 views
Skip to first unread message

AC

unread,
Sep 24, 2021, 4:18:49 AM9/24/21
to Ansible Project
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

Matt Martz

unread,
Sep 24, 2021, 9:27:09 AM9/24/21
to ansible...@googlegroups.com
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`

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d71092c5-a429-4239-9b54-e1cbac5d0dd2n%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

AC

unread,
Sep 24, 2021, 10:07:50 AM9/24/21
to ansible...@googlegroups.com
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"}


Matt Martz

unread,
Sep 24, 2021, 10:13:05 AM9/24/21
to ansible...@googlegroups.com
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"

Reply all
Reply to author
Forward
0 new messages