# Testing file upload with uri?

**URL:** <https://forum.ansible.com/t/testing-file-upload-with-uri/15363>\
**Category:** Ansible Project\
**Created:** [May 29, 2014, 2:07pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363 "2014-05-29T14:07:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_Matz1](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guy\_Matz1](https://forum.ansible.com/u/Guy_Matz1)\
**Post date:** [May 29, 2014, 2:07pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/1 "2014-05-29T14:07:22Z")

</div>

Hi! Anyone know if it’s possible to test the upload of a binary file using the uri module? It doesn’t look like httplib2 has this functionality . . .

Thanks,  
Guy

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 31, 2014, 4:28pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/2 "2014-05-31T16:28:26Z")

</div>

All the arguments are passed on the command line for that module versus loaded form local files, so probably not. Patches to accept a data file to read would be accepted.

---

<div class="post-metadata">

**Author:** ![Guy\_Matz1](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guy\_Matz1](https://forum.ansible.com/u/Guy_Matz1)\
**Post date:** [June 6, 2014, 1:52pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/3 "2014-06-06T13:52:17Z")

</div>

> Hi! How do you feel about something like this for the uri module?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 8, 2014, 3:31pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/4 "2014-06-08T15:31:29Z")

</div>

I actually don’t understand my response to the 1st question of this thread, that may have been a mis-post.

" # if body is a file URL, e.g. ‘file:///bin/false’, read it in and replace the text of body with the binary contents of the file"

Sorry, also not following.

---

<div class="post-metadata">

**Author:** ![Adrian\_Lopez\_Tejedor](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@Adrian\_Lopez\_Tejedor](https://forum.ansible.com/u/Adrian_Lopez_Tejedor)\
**Post date:** [June 9, 2014, 11:19am UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/5 "2014-06-09T11:19:56Z")

</div>

I want to make the same (upload a release to github API with uri module).

I have seen an example posting the contents of a file:

- uri: url=[https://your.jira.example.com/rest/api/2/issue/](https://your.jira.example.com/rest/api/2/issue/)  
method=POST user=your\_username password=your\_pass  
body=“{{ lookup(‘file’,‘issue.json’) }}” force\_basic\_auth=yes

The problem seems that lookup want to read a utf8 file:

/usr/share/pyshared/ansible/runner/lookup\_plugins/file.py  
ret.append(codecs.open(path, encoding=“utf8”).read().rstrip())

I was trying also something like:

- name: read file  
shell: cat issue.rpm

register: file

- uri: url=[http://localhost](http://localhost)  
method=POST  
HEADER\_Content-Type=“application/octet-stream”

body=“{{ file.stdout }}”

But it doesn’t work:  
msg: this module requires key=value arguments …

---

<div class="post-metadata">

**Author:** ![Guy\_Matz](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@Guy\_Matz](https://forum.ansible.com/u/Guy_Matz)\
**Post date:** [June 9, 2014, 4:13pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/6 "2014-06-09T16:13:31Z")

</div>

That ‘file:///’ part is there so that the uri module can be notified that the body data is actually in a file and should be read in from disk . . . I don’t really like that because it seems a little too magical.

I prefer either amending the current file lookup plugin to read binary data if the file is such (currently it is only able to read text files) or adding a bin\_file lookup that is not concerned about utf8, etc.

That make any more sense? ☹

Thanks a lot,  
Guy

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 9, 2014, 8:40pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/7 "2014-06-09T20:40:34Z")

</div>

Ok, yes.

The proposal seems like it should be to make the uri module take an optional “file” parameter, so the lookup plugin doesn’t have to use the content parameter.

This will prevent the need to transfer the file in the arguments, which could get ungainly for large files.

–Michael

---

<div class="post-metadata">

**Author:** ![Guy\_Matz1](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guy\_Matz1](https://forum.ansible.com/u/Guy_Matz1)\
**Post date:** [June 10, 2014, 2:42pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/8 "2014-06-10T14:42:09Z")

</div>

OR - and I think I prefer this - should I create a new lookup\_plugin called rawfile (binfile previously mentioned in this post) that doesn’t do any encoding and simply slurps the file in?

Should this post be in “Ansible Development”?

Thanks again,  
Guy

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 11, 2014, 6:57pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/9 "2014-06-11T18:57:44Z")

</div>

It’s a devel topic mostly, but the answer is no, because lookup plugins are just going to evaluate to string parameters here, and this is an inefficient way to pass large files to remote hosts.

---

<div class="post-metadata">

**Author:** ![Martin\_Etmajer](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@Martin\_Etmajer](https://forum.ansible.com/u/Martin_Etmajer)\
**Post date:** [January 16, 2015, 9:39pm UTC](https://forum.ansible.com/t/testing-file-upload-with-uri/15363/10 "2015-01-16T21:39:55Z")

</div>

I am running into the same issues and I wonder if anyone has come up with a solution to this? +1 from me.
