# File upload from custom HttpApi plugin

**URL:** <https://forum.ansible.com/t/file-upload-from-custom-httpapi-plugin/38408>\
**Category:** Ansible Developer\
**Created:** [September 6, 2023, 6:47am UTC](https://forum.ansible.com/t/file-upload-from-custom-httpapi-plugin/38408 "2023-09-06T06:47:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piotr\_Gabrys](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@Piotr\_Gabrys](https://forum.ansible.com/u/Piotr_Gabrys)\
**Post date:** [September 6, 2023, 6:47am UTC](https://forum.ansible.com/t/file-upload-from-custom-httpapi-plugin/38408/1 "2023-09-06T06:47:42Z")

</div>

Hi, I am developing my first HttpApi plugin and struggling with file upload. I am getting an error:

ansible/module\_utils/connection.py", line 149, in \_exec\_jsonrpc  
raise ConnectionError(  
fatal: [device\_1]: FAILED! =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“filename”: “file.bin”,  
“state”: “present”  
}  
},  
“msg”: “Connection error occurred: Failed to encode some variables as JSON for communication with ansible-connection. The original exception was: Object of type BufferedReader is not JSON serializable”  
}

The way I provide a file to my request is by file-like object - open() in “rb” mode  
I’ve also tried reading the file-like object and providing it this way  
None of these approaches work

I was able to get it working using requests library with:  
response = session.put(url, data=open(“file.bin”, “rb”), headers={“Content-Type”: “application/octet-stream”})

Is is possible to upload a file using custom HttpApi plugin?  
If so how do I provide the file to a request?

If any additional info is needed, let me know

[Code snippet](https://gist.github.com/Soothly/c7dba5e626fd4f1b6b172940ea6ce9a6)

Best Regards  
Piotr Gabryś
