8

I'm trying to test the upload of a file and its metadata using Python and FastAPI.

Here is how I defined the route for the upload:

@app.post("/upload_files")
async def creste_upload_files(uploaded_files: List[UploadFile], selectedModel: str = Form(...),
                              patientId: str = Form(...), patientSex: str = Form(...),
                              actualMedication: str = Form(...), imageDim: str = Form(...),
                              imageFormat: str = Form(...), dateOfScan: str = Form(...)):
    for uploaded_dicom in uploaded_files:
        upload_folder = "webapp/src/data/"
        file_object = uploaded_dicom.file
        #create empty file to copy the file_object to
        upload_folder = open(os.path.join(upload_folder, uploaded_dicom.filename), 'wb+')
        shutil.copyfileobj(file_object, upload_folder)
        upload_folder.close()
    return "hello"

(I'm not using the metadata but I will later).

I use unittest for the testing:

class TestServer(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(app)
        self.metadata = {
            "patientId": "1",
            "patient_age": "M",
            "patientSex": "59",
            "patient_description": "test",
            "actualeMedication": "test",
            "dateOfScan": datetime.strftime(datetime.now(), "%d/%m/%Y"),
            "selectedModel": "unet",
            "imageDim": "h",
            "imageFormat": "h"
        }

    def tearDown(self):
        pass

    def test_dcm_upload(self):
        dicom_file = pydicom.read_file("tests/data/1-001.dcm")
        bytes_data = dicom_file.PixelData
   
        files = {"uploaded_files": ("dicom_file", bytes_data, "multipart/form-data")}
        response = self.client.post(
            "/upload_files",
            json=self.metadata,
            files=files
        )
        print(response.json())

But it seems that the upload doesn't work, I get the following print of the response:

{'detail': [{'loc': ['body', 'selectedModel'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'patientId'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'patientSex'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'actualMedication'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'imageDim'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'imageFormat'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'dateOfScan'], 'msg': 'field required', 'type': 'value_error.missing'}]}

It is possible that I should upload using a Formdata instead of the body request (json=self.metadata) but I don't know how it should be done.

1
  • I also want to test with formData, have you found any method? I am trying but no lib supporting that. Commented Feb 19, 2023 at 16:48

1 Answer 1

13

The answer is just to replace json=self.metadata which can be used for body parameters by data=self.metadata for a formData

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.