1

I have the following fastapi endpoint

@app.post("/process-file")
async def process_file(
    filename: str,
    config: Config,
    file: UploadFile = File(...),
) -> StreamingResponse:

I would like to send a request to it using the python requests library, but can't figure out how to properly format the request.

I attempted to send the filename as a param and the config as json as so

config = {
    "threshold": 0.5,
    "black_background": True,
    "bounding_boxes": False,
    "text": True,
    "alpha": 1,
    "beta": 0.6,
    "gamma": 0,
}

for filename in os.listdir(INPUT_DIR):
    path = os.path.join(INPUT_DIR, filename)
    with open(path, "rb") as f:
        print(f"Sending {filename} to the API...")
        # send the file to the API endpoint
        params = {"filename": filename}
        files = {"file": f}
        response = requests.post(
            API_URL_FILE,
            params=params,
            json=config,
            files=files,
        )

However this results in the error: Error: 422, [{'loc': ['body', 'config'], 'msg': 'field required', 'type': 'value_error.missing'}]

0 Answers0