I am using python to upload a file to an api with
url = 'http://domain.tld/api/upload'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, files=files)
this works well and my file is uploaded to the server as image.jpg. Now I don't have a local files but an uri instead, so I changed my code to:
url = 'http://domain.tld/api/upload'
files = {'file': urlopen('http://domain.tld/path/to/image.jpg')}
r = requests.post(url, files=files)
the image is also uploaded sucessfully but it does not preserve it's name and is stored as 'file' (without extension). My question is, how can I upload an url while keeping it's filename (Of course without downloading it first)
You can pass the name:
files = {'name': ('image.jpg', urlopen('http://domain.tld/path/to/image.jpg'))}
If you look at the post body you will see for your variation:
Content-Disposition: form-data; name="file"; filename="file"
And using the code above:
Content-Disposition: form-data; name="name"; filename="image.jpg"
You can see the name is retained in the latter.
Related
I am trying to put a file to a WebDav enabled URL.
The code looks like this:
headers = {'Authorization':'Basic', 'username': 'doc_iconx', 'password': 'doc_iconx'}
id = "SOMEID"
pw = "SOMEPW"
try:
url = 'https://mywebsite.com/Dir/'
files = {'upload_file': open(fileName, 'rb')}
r = requests.put(url,auth=HTTPDigestAuth(id,pw), files=files, headers={'User-Agent': 'Mozilla'
})
I get back:
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>
I know the ID/Password is good because I can do a put using curl
Any Ideas?
Since your authentication scheme is using Basic, all you would have to do is to use HTTPBasicAuth instead of HTTPDigestAuth:
r = requests.put(url,auth=HTTPBasicAuth(id,pw), files=files, headers={'User-Agent': 'Mozilla'})
for which requests actually has even a shortcut, by not specifying the mode:
r = requests.put(url,auth=(id,pw), files=files, headers={'User-Agent': 'Mozilla'})
I had two different problems going on. Sal, corrected my Auth error. The second error was a stupid user error. I need to append the filename I wanted uploaded to the end of the URL. The way it was constructed put was trying to create a file named Report. However Report is an existing directory, where I intended to write the file.
I am trying to create a response that will return 3 files with one request.
However, if you give it to the response body, I am in trouble because I do not know whether it can be realized.
The method of generating the response body that I am about to deliver is trying to return using python's MultipartEncoder
[ response body ]
※Boundary generation is also done
--dd7457a7dc684f32b2fd26ec468ed4b8
Content-Disposition: form-data; name=file1; filename="test1"
Content-Type: application/octet-stream
test1 sample
--dd7457a7dc684f32b2fd26ec468ed4b8
Content-Disposition: form-data; name=file2; filename="test2"
Content-Type: application/octet-stream
test2 sample
--dd7457a7dc684f32b2fd26ec468ed4b8
Content-Disposition: form-data; name=file3; filename="test3"
Content-Type: application/octet-stream
test3 sample
--dd7457a7dc684f32b2fd26ec468ed4b8--
Body as above
The following header
response.headers["Content-Type"] = 'multipart/form-data`
I know that swagger-ui.js creates a download link with the fileapi's blob library, but download three files via the download link of three files or one download link using the blob library it can
Is there such a way?
It is already possible to do a method of consolidating files into tar or zip and then doing DL and json format.
I would like to ask if there is any way.
[version]
swagger-ui 2.2.10
Python 3.4.4
flask 0.10.1
I want to search the Google Drive folder.
To check if there is not a folder with the name of the folder to create.
I have this code:
request_url = "https://www.googleapis.com/drive/v2/files?access_token=%s" % (access_token)
data = {"q": "title=filename"}
data_json = json.dumps(data)
print data_json
req = urllib2.Request(request_url,data_json, headers)
print request_url
print data_json
content = urllib2.urlopen(req).read()
print content
content = json.loads(content)
Unfortunately, there is no research but creates a no name file.
Thank you in advance for your help
Basically with urllib2, when you specify the data while using Request(), it automatically considers your request as a POST request, and that's why it creates a file with no name.
If you want it to be a GET request, you have to encode the data in the URL before calling Request()
On my Django backend, I deal with images like this:
for file in request.FILES.iteritems():
image = request.FILES["image"]
It is easy to send a post request via java using a byte array.
But how to do a post request using Python?
As in, I've an image url.
I've downloaded the iamge from an url using
r = requests.get('url')
r.content #ImageConent
Now how do I post it such that it is delivered as a byte array?
Have a look at the requests documentation on how to send multipart requests. Basically you just need to do:
>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)
Or in your case:
>>> r = requests.get(url1)
>>> files = {'image': r.content}
>>> r = requests.post(url2, files=files)
I'm trying to rewrite some old python code with requests module.
The purpose is to upload an attachment.
The mail server requires the following specification :
https://api.elasticemail.com/attachments/upload?username=yourusername&api_key=yourapikey&file=yourfilename
Old code which works:
h = httplib2.Http()
resp, content = h.request('https://api.elasticemail.com/attachments/upload?username=omer&api_key=b01ad0ce&file=tmp.txt',
"PUT", body=file(filepath).read(),
headers={'content-type':'text/plain'} )
Didn't find how to use the body part in requests.
I managed to do the following:
response = requests.put('https://api.elasticemail.com/attachments/upload',
data={"file":filepath},
auth=('omer', 'b01ad0ce')
)
But have no idea how to specify the body part with the content of the file.
Thanks for your help.
Omer.
Quoting from the docs
data – (optional) Dictionary or bytes to send in the body of the Request.
So this should work (not tested):
filepath = 'yourfilename.txt'
with open(filepath) as fh:
mydata = fh.read()
response = requests.put('https://api.elasticemail.com/attachments/upload',
data=mydata,
auth=('omer', 'b01ad0ce'),
headers={'content-type':'text/plain'},
params={'file': filepath}
)
I got this thing worked using Python and it's request module. With this we can provide a file content as page input value. See code below,
import json
import requests
url = 'https://Client.atlassian.net/wiki/rest/api/content/87440'
headers = {'Content-Type': "application/json", 'Accept': "application/json"}
f = open("file.html", "r")
html = f.read()
data={}
data['id'] = "87440"
data['type']="page"
data['title']="Data Page"
data['space']={"key":"AB"}
data['body'] = {"storage":{"representation":"storage"}}
data['version']={"number":4}
print(data)
data['body']['storage']['value'] = html
print(data)
res = requests.put(url, json=data, headers=headers, auth=('Username', 'Password'))
print(res.status_code)
print(res.raise_for_status())
Feel free to ask if you have got any doubt.
NB: In this case the body of the request is being passed to the json kwarg.