I would like to upload an image file and some text to my API end point, I have a Flask API server to handle post requests from React Native APP and unit test script, but I don't know why I can only get the data from request.form instead of request.files, and the request.files is ImmutableMultiDict([])
and the request.form is ImmutableMultiDict([('image', "<FileStorage: 'test.jpg' ('image/jpeg')>")])
which the value in request.form['image']
is str instead of FileStorage object. Here is my code:
unit_test.py
import io
import unittest
from api import app
class FlaskAppTests(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.app.testing = True
with open('image/test.jpg', 'rb') as img:
self.binary_image = img.read()
def test_facial_authentication_response(self):
response = self.app.post('/facial_authentication', content_type='multipart/form-data', data={
'image': (self.binary_image, 'test.jpg', 'image/jpeg')
})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content_type, 'application/json')
if __name__ == '__main__':
unittest.main()
React Native code:
await fetch('http://api-end-point', {
method: 'POST',
headers: {'Content-Type': 'multipart/form-data'},
body: formData,
}).then(res => res.json()).then(json => {
console.log(json);
}).catch((error) => {
console.error(error);
});
};
Flask app.py:
import io
from flask import Flask, request
app = Flask(__name__)
CORS(app)
@app.route('/', methods=['POST'])
def api():
image_from_form = request.files['image'] # error raised cause the dict is empty
image_from_files = request.form['image'] # return "<FileStorage: 'test.jpg' ('image/jpeg')>"
print(image_from_form, " and ", image_from_files)
# ImmutableMultiDict([]) and ImmutableMultiDict([('image', "<FileStorage: 'test.jpg' ('image/jpeg')>")])
with open(os.path.join(dirname, 'temp.jpg'), "wb") as file:
file.write(binary_image)
The image itself is binary/Blob object, depends on the client side. I have tried to ask ChatGPT or google from the anwser, but almost results are only mention that the request.files should contain files if the content-type and file field is correct.