Page 1 of 1

How to send file using urequests.

Posted: Sun May 19, 2019 6:32 am
by r2xxd2
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"

Re: How to send file using urequests.

Posted: Sun May 19, 2019 10:26 am
by jimmo
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`).

Re: How to send file using urequests.

Posted: Thu May 23, 2019 4:45 am
by r2xxd2
Thank you!

Re: How to send file using urequests.

Posted: Fri May 24, 2019 3:55 am
by r2xxd2
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.

Re: How to send file using urequests.

Posted: Fri May 24, 2019 6:07 am
by jimmo
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)

Re: How to send file using urequests.

Posted: Thu Nov 21, 2019 8:20 pm
by zvafqthr
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?

Re: How to send file using urequests.

Posted: Sun Apr 05, 2020 9:55 pm
by Huntaway55
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.

Re: How to send file using urequests.

Posted: Mon Apr 05, 2021 3:06 pm
by johncblacker
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...

Re: How to send file using urequests.

Posted: Thu May 06, 2021 3:29 pm
by SpotlightKid
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