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
Related
I've written an API to return a CSV to a user based on content they fill into a form in a web application. When running the code below, I can see the response in my console and am certain that the content is properly being returned by my API, but I can't seem to get the file to automatically start downloading.
csv = final_df.to_csv()
response = make_response(csv)
response.headers['Content-Disposition'] = "attachment; filename=export.csv"
response.headers['Content-type'] = "application/force-download"
return response
A working example of mine differs by quoting the filename (which the Developer Docs hint at being required), and using a correct mimetype.
Try
return bytes(csv, encoding='UTF-8'), 200, {
'Content-Type': 'text/csv',
'Content-Disposition': 'attachment; filename="export.csv"'}
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)
Is it possible to generate the downloadable link for a private file uploaded in the google drive?
I have tried with the files API and generated the 'webContent Link', but it is accessible only to the owner and the shared users.
(Expecting a public link that can be shared with anyone)
def file_info_drive(access_token, file_id):
headers = {'Authorization': 'Bearer ' + access_token, "content-type": "application/json"}
response = requests.get('https://www.googleapis.com/drive/v2/files/{file_id}', headers=headers)
response = response.json()
link = response['webContentLink']
return link
You want to make anyone download the file using webContentLink.
You want to use Drive API v2.
You want to achieve this using 'request' module with Python.
You have already been able to upload and download the file using Drive API.
If my understanding is correct, how about this modification?
Modification points:
In order to make anyone download the file using webContentLink, it is required to share publicly the file.
In this modified script, the file is publicly shared with the condition of {'role': 'reader', 'type': 'anyone', 'withLink': True}. In this case, the persons who know the URL can download the file.
Modified script:
When your script is modified, it becomes as follows.
def file_info_drive(access_token, file_id):
headers = {'Authorization': 'Bearer ' + access_token, "content-type": "application/json"}
# Using the following script, the file is shared publicly. By this, anyone can download the file.
payload = {'role': 'reader', 'type': 'anyone', 'withLink': True}
requests.post('https://www.googleapis.com/drive/v2/files/{file_id}/permissions', json=payload, headers=headers)
response = requests.get('https://www.googleapis.com/drive/v2/files/{file_id}', headers=headers)
response = response.json()
link = response['webContentLink']
return link
Note:
In this case, the POST method is used. So if an error for the scopes occurs, please add https://www.googleapis.com/auth/drive to the scope.
Reference:
Permissions: insert
If I misunderstood your question and this was not the direction you want, I apologize.
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.
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.