How to upload file via sending a post request with a path to the file in Kotlin?
This is what I did in Python, it manage to work, it is plain and simple.
import requests
//file that I want to upload
path = 'download/special.db'
files = {'file': open(path, 'rb')}
r = requests.post('http://myurl/newfile/Upload', files=files)
print (r.text)
This code work and manage to print the response
I need to write one version for my Android app, is it possible to write something similar in Kotlin? send a post request and attach a files=files to my request and send. The problem, what is the kotlin equivalentof that? is it possible to write everything on one activity?
what should I write for files = {'file': open(path, 'rb')} in kotlin?
Search online, most of the method is use Retrofit, which is too complicated.
Related
;TLDR
I want to send a file with requests.send() using multipart/form-data request without storing the file on a hard drive. Basically, I'm looking for an alternative for open() function for bytes object
Hello, I'm currently trying to send multipart/form-data request and pass in-memory files in it, but I can't figure out how to do that.
My app receives images from one source and sends them to another. Currently it sends get request directly to file, (e.g. requests.get('https://service.com/test.jpeg')), reads image's bytes and writes them into new file on the hard drive. The sending code that works looks like this:
def send_file(path_to_image: str)
url = get_upload_link()
data = {'photo': open(path_to_image, 'rb')}
r = requests.post(url, files=data)
send_file("test.jpeg")
The main issue I have with this approach is that I have to keep files on my hard drive. Sure, I can use my drive as some sort of a "temporary buffer" and just delete them after I no longer need these files, but I believe there's much more simple way to do that.
I want my function to receive bytes object and then send it. I actually tried doing that, but the backend doesn't accept them. Here's what I tried to do
Attempt 1
def send_file(image: bytes)
url = get_upload_link()
data = {'photo': open(image, 'rb')}
r = requests.post(url, files=data)
I get "ValueError: embedded null byte"
Attempt 2
def upload_photo(image: bytes):
url = get_upload_link()
file = BytesIO(image)
data = {'photo': file}
r = requests.post(url, files=data)
Backend server doesn't process my files correctly. It's like passing files=None, same response
I also tried:
sending the returning value of the methods: file.getbuffer() and file.read()
file.write(image) and then sending file
StringsIO object
etc.
Final notes
I noticed, that open() returns _io.BufferedReader object. I also looked for a way to construct its instance, but couldn't fund a way. Can someone help me, please?
UPD:
If anyone is interested, the receiving api is this
From the official documentation:
POST a Multipart-Encoded File
...
If you want, you can send strings to
be received as files:
url = 'https://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files)
I want to upload short videos through an API connection (which one is not relevant for the question). The videos that will be uploaded are already on a server (publicly accessible, so that is not the issue) with a direct link (eg: 'https://nameofcompany.com/uploads/videoname.mp4").
I am using the Requests library, so the post request looks like this:
requests.post(url, files={'file': OBJECT_GOES_HERE}, headers=headers)
The object should be a 'bytes-like object', so with a local file we can do:
requests.post(url, files={'file': open('localfile.mp4', 'rb')}, headers=headers)
I tested this with a local file and this works. However, as mentioned I need to upload it from the link, so how do I do that? Is there some method (or some library with a method) that would return the same type of response like the open() method does for local files? If not, how could I create one myself?
import requests
from io import BytesIO
url = 'https://nameofcompany.com/uploads/videoname.mp4'
r = requests.get(url)
video = r.content
# This is probably enough:
requests.post(url2, files={'file': video}, headers=headers)
# But if not, here's an example of using BytesIO to treat bytes as a file:
requests.post(url2, files={'file': open(BytesIO(video), 'rb')}, headers=headers)
i'm trying to POST upload large files with multipart/form-data encoded, but i want to use requests module only, not using requests_toolbelt.
is there anyway i can upload the file by chunking it to reduce memory usage?
here's my current code
with open(file, 'rb') as f:
r = requests.post('url', files={'report.mp4': f})
print(r.text)
the reason i don't want to use requests_toolbelt, is that when i request the POST the code finishes running, it doesn't give any error, it just finishes and the request text is empty
here's the code that i'm using that fails.
m = MultipartEncoder(
fields={'file': ('filename', open(file, 'rb'), 'video/mp4')}
)
r = requests.post(url, data=m,
headers={'Content-Type': m.content_type})
I am using Python's Requests library to POST a PDF to a document store, the uploaded PDF is thereafter used in a signature process. However when uploading the PDF using Python (in stead of CURL) the signing environment doesnt work. On comparing different files, I found out that Requests adds some data to the PDF:
--ca9a0d04edf64b3395e62c72c7c143a5
Content-Disposition: form-data; name="LoI.pdf"; filename="LoI.pdf"
%%Original PDF goes here%%
--ca9a0d04edf64b3395e62c72c7c143a5--
This data is accepted perfectly fine by different PDF readers, but not by the Signature API. Is there a way to prevent Requests from adding this data to the PDF? I used the following code:
myfile = request.FILES['myfile']
url = %%documentstoreURL%%
resp = requests.request('post', url, files={myfile.name:myfile}, headers={'Content-Type':'application/pdf'}, auth=(%%auth details%%))
Thanks!
You're sending the file as binary data with curl, but attaching it in requests.
I read over the source code, and I believe resp = requests.request('post', url, data={myfile.name:myfile}, headers={'Content-Type':'application/pdf'}, auth=(%%auth details%%)) (data instead of files) will avoid the multipart encoding.
At the very least, it should be differently broken.
Being guided in the right direction, I found a working solution based on Python requests - POST data from a file
In the end I did it as follows:
myfile = request.FILES['myfile']
payload = request.FILES['myfile'].read()
headers = {'content-type': 'application/pdf'}
url = "%%DocumentServiceURL"
r = requests.post(url, auth=(%%auth_details%%), data=payload, verify=False, headers=headers)
I have tried to upload a pdf by sending a POST Request to an API in R and in Python but I am not having a lot of success.
Here is my code in R
library(httr)
url <- "https://envoc-apply-api.azurewebsites.net/api/apply"
POST(url, body = upload_file("filename.pdf"))
The status I received is 500 when I want a status of 202
I have also tried with the exact path instead of just the filename but that comes up with a file does not exist error
My code in Python
import requests
url ='https://envoc-apply-api.azurewebsites.net/api/apply'
files = {'file': open('filename.pdf', 'rb')}
r = requests.post(url, files=files)
Error I received
FileNotFoundError: [Errno 2] No such file or directory: 'filename.pdf'
I have been trying to use these to guides as examples.
R https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html
Python http://requests.readthedocs.io/en/latest/user/quickstart/
Please let me know if you need any more info.
Any help will be appreciated.
You need to specify a full path to the file:
import requests
url ='https://envoc-apply-api.azurewebsites.net/api/apply'
files = {'file': open('C:\Users\me\filename.pdf', 'rb')}
r = requests.post(url, files=files)
or something like that: otherwise it never finds filename.pdf when it tries to open it.