keep filename while uploading an url with python response library
<p>I am using python to upload a file to an api with</p>

<pre><code>url = 'http://domain.tld/api/upload'
files = {'file': open('image.jpg', 'rb')}

r = requests.post(url, files=files)
</code></pre>

<p>this works well and my file is uploaded to the server as <code>image.jpg</code>. Now I don't have a local files but an uri instead, so I changed my code to:</p>

<pre><code>url = 'http://domain.tld/api/upload'
files = {'file': urlopen('http://domain.tld/path/to/image.jpg')}

r = requests.post(url, files=files)
</code></pre>

<p>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)</p>

<p>You can pass the name:</p>

<pre><code> files = {'name': ('image.jpg', urlopen('http://domain.tld/path/to/image.jpg'))}
</code></pre>

<p>If you look at the post body you will see for your variation:</p>

<pre><code>Content-Disposition: form-data; name="file"; filename="file"
</code></pre>

<p>And using the code above:</p>

<pre><code>Content-Disposition: form-data; name="name"; filename="image.jpg"
</code></pre>

<p>You can see the name is retained in the latter.</p>

python python-requests