11

I have made an api through flask which submit a file and after receiving it on the back end , I want to further upload this to an unknown server using python requests module

The server accepts the file if I do this like -

requests.post(urlToUnknownServer,files={'file':open('pathToFile')})

Now my requirement is to get the file param uploaded through flask.
In flask I get the file as FileStorage object. I don't want to save this on my server, instead directly wants it to upload further.
So basically I want to convert that FileStorage object to return type of open() function which is file(please correct me here if I am wrong)

I had tried it using -

obj=file(request.files['fileName'].read().encode('string-escape'))
requests.post(urlToUnknownServer,files={'file':obj})

This doesn't work. Can it be possible to do that without saving file on my server.

sagar
  • 725
  • 2
  • 13
  • 30
  • 1
    Hi Sagar, I am going through the same issue. Did you find solution? I want to extract the file from file storagr object. – dhinar1991 Apr 17 '18 at 05:30
  • @Sagar I am also stack with this issue. I am using postman to send file to flask api. then my flask api send file to 3rd party api. for example **r = requests.post(url, headers=headers, data=payload, files={'file': file.read()})** . I am getting issue at 3rd party api. 3rd party api says invalid image extension...I am sending same image direct to 3rd party api using postman then it works...Help me. – Naisarg Parmar Jan 28 '19 at 09:07

3 Answers3

8

Maybe you can use read() without encoding it. such as this:

obj=request.files['fileName'].read()
requests.post(urlToUnknownServer,files={'file':obj})
T . Tom Cat
  • 104
  • 1
  • 5
  • I am using postman to send file to flask api. then my flask api send file to 3rd party api. for example **r = requests.post(url, headers=headers, data=payload, files={'file': file.read()})** . I am getting issue at 3rd party api. 3rd party api says invalid image extension...I am sending same image direct to 3rd party api using postman then it works...Help me. – Naisarg Parmar Jan 28 '19 at 09:04
  • @NaisargParmar Are you sending all the headers through flask that is being sent by postman ? – sagar Jan 28 '19 at 11:30
  • @sagar Thankx for reply..I am sending this request to 3rd party api **r = requests.post(url, headers=headers, data=payload, files={'file': (file.filename, file.read(), file.content_type)})**. Now with this post request, at-least I am able to create file to their server..but the content of the file is empty. It means **file.read()** is not working perfectly – Naisarg Parmar Jan 28 '19 at 11:55
  • @NaisargParmar any luck with empty file? – Prerit Mohan Mar 04 '21 at 13:44
  • @PreritMohan I did little hack. 1. I save file in backend. 2. Read that file. 3. Attached that file to 3rd party api. 4. Delete file in backend. # Save File file = request.files['file'] file.save(secure_filename(file.filename)) # Send File to 3rd party API r = requests.post(url, headers=headers, data=payload, files={'file': (file.filename, open(file.filename, 'rb'), file.content_type)}) # Delete file os.remove(file.filename) – Naisarg Parmar Mar 09 '21 at 04:55
6

For anyone stuck with same problem, just convert to BufferedReader

like this:

    from io import BufferedReader
    image = request.files.get('name')
    image.name = image.filename
    image = BufferedReader(image)

and then you can post it

    requests.post(urlToUnknownServer,files={'file' : image})
-2

Try without file() constructor:

obj = request.files['fileName'].read().encode('string-escape')
requests.post(urlToUnknownServer,files={'file':obj})
mhd
  • 4,561
  • 10
  • 37
  • 53