Submitting binary data as a file via post request - python

I know how to submit a post request with a file.
files = {'file': open('local.pdf', 'rb')}
r = requests.post(url, files=files)
Since I'm downloading the file from a response, I want to avoid writing the response.contents to my local disk ('local.pdf') before I submit the post request. Can I submit the file as a bytes object?

You can use io.BytesIO to do that.
Here is an example:
rawData = io.BytesIO(b"Some data: \x00\x01") # Change the content
files = {'file': rawData}
r = requests.post(url, files=files)

Related

Python: open a video file from an url the same way open("filepath", "rb") does for local 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)

How to POST an MP4 using Python requests?

I am trying to upload an MP4 file, using requests.post. I'm not sure if it is due to the size of the file, as it is 3.2MB and I have seen people saying they have issues over 1.5MB. Any help is appreciated. Thanks
def post_video(URL):
files = {'file': open(file_path, 'rb')}
response = requests.post(URL, files=files)
return response
The response code should 204, however it is returning 500. The request works on Swagger UI
It won't let me comment,
The most likely cause is that you're not adding a header to your post that is required. I'd look at the REST API's documentation or even the Swagger UI and enter the developer console, watch the output and ensure you're adding everything needed.
Headers usually include the content type and sometimes include an authentication token, have you authenticated with the end point?
Here is an example of my proposition:
def getresponse:
userpass = b64encode(b"<username>:<password>").decode("ascii")
headers = {'Content-type':'video/mp4', 'Authorization': 'Basic ' + userpass}
files = {'file': open(file_path, 'rb')}
response = requests.post(URL, files=files, headers=headers)
return response

How to post a file as multipart/form-data in Kotlin?

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.

How to upload a pdf by sending a POST Request to an API

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.

How to Send post response as image from python requests

i am trying to get my image hosted online and for that i am using python
import requests
url = 'http://imgup.net/'
data = {'image[image][]':'http://www.webhost-resources.com/wp-content/uploads/2015/01/dedicated-hosting-server.jpg'}
r = requests.post(url, files=data)
i am not able to get the response url of the hosted image from the response .
Please help !
The files parameter of requests.post needs a:
Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.
There's more data you'll need to send than just the file, most importantly the "authenticity token". If you look at the source code of the page, it'll show you all other parameters as <input type="hidden"> tags.
The upload URL is http://imgup.net/upload, as you can see from the action attribute of <form>.
So what you need to do is:
Download the image you want to upload (I'll call it dhs.jpg).
Do a GET request of the main page, extracting the authenticity_token.
Once you have that, send the request with files= and data=:
‌
url = "http://imgup.net/upload"
data = {'utf8': '✓', 'authenticity_token': '<put your scraped token here>', '_method': 'put'}
f = open("dhs.jpg", "rb") # open in binary mode
files = {'image[image][]': f}
r = requests.post(url, files=files, data=data)
f.close()
print(r.json()["image_link"]
Final note: While I couldn't find any rule against this behaviour in their T&C, the presence of an authenticity token makes it seem likely that imgup doesn't really want you to do this automatically.

Categories

Resources