How to send file using urequests.
How to send file using urequests.
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"
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.
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`).
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.
Thank you!
Re: How to send file using urequests.
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.
Thanks.
Re: How to send file using urequests.
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
Here's
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)
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"'}
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'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.
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?
-
- Posts: 1
- Joined: Sun Apr 05, 2020 9:51 pm
Re: How to send file using urequests.
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.
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.
-
- Posts: 9
- Joined: Sun Nov 11, 2018 8:11 pm
Re: How to send file using urequests.
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...
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...
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: How to send file using urequests.
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
https://github.com/SpotlightKid/micropy ... /upload.py