I'm trying to upload a file along with some data to a Flask server using Python Requests.
Here's my code:
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
temp_image = request.files['file']
filename = secure_filename(temp_image.filename)
temp_image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return 'Success'
url = "https://some-random-url.com/"
files = {'file': open("shared/assets/12332.png", 'rb')}
data = {"lobby_id": "lobby_id"}
r = requests.post(url=url, files=files)
So this part works absolutely fine and I get <Response [200]>
.
But when I include the data
part, and modify my Flask part of the code to,
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
temp_image = request.files['file']
data = request.get_json(force=True)
filename = secure_filename(temp_image.filename)
temp_image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return 'Success'
I get <Response [400]>
. I'm assuming there's a mistake in the Flask section of the code but I'm unable to figure it out.
Bad Request
The browser (or proxy) sent a request that this server could not understand.
```