Flask: Sending File Between Two Servers Results in Empty `request.files` on Receiver Server

17 Views Asked by At

I am attempting to send a file from one Flask server (Server 1) to another Flask server (Server 2) and then back again. However, when the file reaches Server 2, the request.files object appears to be empty, resulting in a KeyError when attempting to access it.

Here is the basic setup of my servers:

Server 1:

from flask import Flask, request
import requests

app = Flask(__name__)

@app.route('/test', methods=['POST'])
def test():
    file = request.files['file'].read()
    print(file)
    
    response = requests.post(url="http://127.0.0.1:5002/test",
                             headers=request.headers,
                             files={'file': file}
                             )
    
    return response.json(), response.status_code

if __name__ == "__main__":
    app.run(port=5000)

Server 2:

from flask import Flask, request

app = Flask(__name__)

@app.route('/test', methods=["POST"])
def test():
    file = request.files['file']  # <-- KeyError occurs here
    print(file)
    
    return 1, 200

if __name__ == "__main__":
    app.run(port=5002, debug=True)

This is the error

127.0.0.1 - - [23/Mar/2024 10:09:02] "POST /test HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\gratu\anaconda3\envs\ML\lib\site-packages\flask\app.py", line 1488, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\gratu\anaconda3\envs\ML\lib\site-packages\flask\app.py", line 1466, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\gratu\anaconda3\envs\ML\lib\site-packages\flask\app.py", line 1463, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\gratu\anaconda3\envs\ML\lib\site-packages\flask\app.py", line 872, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\gratu\anaconda3\envs\ML\lib\site-packages\flask\app.py", line 870, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\gratu\anaconda3\envs\ML\lib\site-packages\flask\app.py", line 855, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
  File "C:\Users\gratu\Projects\flasktest\app2.py", line 10, in test
    file = request.files['file']
  File "C:\Users\gratu\anaconda3\envs\ML\lib\site-packages\werkzeug\datastructures\structures.py", line 192, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'file'

When a file is sent from Server 1 to Server 2, the request.files object on Server 2 appears to be empty, causing a KeyError. I've confirmed that the file is being sent correctly from Server 1 as I can see the request.files dictionary with the files I have sent it when I print it.

I'm unsure why the request.files object is empty on Server 2. Is there something I'm missing in my setup or code? Any insights or suggestions on how to resolve this issue would be greatly appreciated. Thank you!

1

There are 1 best solutions below

0
Gratus Richard On

Turns out the issue was passing on the header. When I removed the header, it started working properly.

from flask import Flask, request
import requests

app = Flask(__name__)

@app.route('/test', methods=['POST'])
def test():
    file = request.files['file'].read()
    print(file)
    
    response = requests.post(url="http://127.0.0.1:5002/test",

                             files={'file': file}
                             )
    
    return response.json(), response.status_code

if __name__ == "__main__":
    app.run(port=5000)