How to send file using urequests.

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
r2xxd2
Posts: 3
Joined: Sun May 19, 2019 12:42 am

How to send file using urequests.

Post by r2xxd2 » Sun May 19, 2019 6:32 am

I need help. I'm trying to send a jpg file as Telegram bot.
In Python we can use construction like this:
import requests
url = 'https://api.telegram.org/botXXXXXX/sendPhoto'
f = {'photo': open("sd/photo.jpg", "rb")}
data = {'chat_id' : 'XXXXXXX'}
requests.post(url, data=data, files=f)
How can we upload file using "urequests"

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to send file using urequests.

Post by jimmo » Sun May 19, 2019 10:26 am

The urequests module doesn't include all of the features of the one from big Python.

But you can do it manually by crafting your own POST request using the multipart/form-data Content-Type.

See https://developer.mozilla.org/en-US/doc ... ST#Example for an example

And depending on how far you feel like following the code path through the Python requests library, you can see their implementation. Here's a starting point https://github.com/kennethreitz/request ... /models.py (see `def _encode_files`).

r2xxd2
Posts: 3
Joined: Sun May 19, 2019 12:42 am

Re: How to send file using urequests.

Post by r2xxd2 » Thu May 23, 2019 4:45 am

Thank you!

r2xxd2
Posts: 3
Joined: Sun May 19, 2019 12:42 am

Re: How to send file using urequests.

Post by r2xxd2 » Fri May 24, 2019 3:55 am

Sorry, but I still need help... could anyone please share the real example or project how to use multipart/form-data Content-Type and what additional modules or functions I need to create for sending file through POST request.
Thanks.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to send file using urequests.

Post by jimmo » Fri May 24, 2019 6:07 am

Can you share what you've got so far. But here are some hints...

So you're going to need to set up a headers dict to pass to `requests.post` (which sets the content-encoding) and then it's a matter of generating a string to pass as the `data` parameter which is the encoded parameters (chat_id and files).

Adapting the example from the page I linked to

Code: Select all

headers = {'Content-Type': 'multipart/form-data;boundary="boundary"'}
Here's

Code: Select all

--boundary 
Content-Disposition: form-data; name="chat_id" 

XXXXXXX
--boundary 
Content-Disposition: form-data; name="photo"; filename="foo.jpg" 

<contents of foo.jpg>
--boundary--
You can build this up by appending to a bytearray or something.

You'll want to use something a bit more unique for the boundary (generate some random string maybe). Perhaps read the spec linked from that page (https://tools.ietf.org/html/rfc2046#section-5.1.1)

zvafqthr
Posts: 1
Joined: Thu Nov 21, 2019 8:15 pm

Re: How to send file using urequests.

Post by zvafqthr » Thu Nov 21, 2019 8:20 pm

Hey. I also had the need to send files through the telegram bot, I read the topic and did not understand how this can be implemented on a micropython. Can you write example of request?

Huntaway55
Posts: 1
Joined: Sun Apr 05, 2020 9:51 pm

Re: How to send file using urequests.

Post by Huntaway55 » Sun Apr 05, 2020 9:55 pm

I came across this thread when was looking to upload files in micropython. Following the requests library source, I was able to hack something together to get file uploading to work in micropython urequests.
I've pushed the code to Github.

https://github.com/jono-allen/MQTT-Cam/ ... ploader.py

I'll also push the app that controls the board in the next week when I finish the AWS cloudformation side.

johncblacker
Posts: 9
Joined: Sun Nov 11, 2018 8:11 pm

Re: How to send file using urequests.

Post by johncblacker » Mon Apr 05, 2021 3:06 pm

How do I get the "contents" of the file included? Do I:
f = open('foo.jpeg', 'rb')
dat = f.read()

and then include that "dat" into the multipart/form-data between boundary statements?

Thanks for the help...

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: How to send file using urequests.

Post by SpotlightKid » Thu May 06, 2021 3:29 pm

In my mrequests project (bascially urequests with many fixes and improvements) I have an example HTTP upload script:

https://github.com/SpotlightKid/micropy ... /upload.py

Post Reply