How do I download a file from FastAPI backend using JavaScript Fetch API in the frontend?

First, you need to adjust your endpoint on server side to accept path parameters, as in the way that is currently defined, lat and long are expected to be query parameters instead; however, in the JavaScript code you provided, it seems that you are trying to send those coordinates as path parameters. Thus, your endpoint … Read more

Download PDF file using pdfkit and FastAPI

Returning FileResponse is solved my problem. Thanks to @Paul H and @clmno Below codes are working example of returning pdf file to download with FastApi. from typing import Optional from fastapi import FastAPI from starlette.responses import FileResponse import pdfkit app = FastAPI() config = pdfkit.configuration(wkhtmltopdf=r”C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe”) @app.get(“/”) def read_root(): pdfkit.from_url(“https://nakhal.expo.com.tr/nakhal/preview”,”file.pdf”, configuration=config) return FileResponse( “file.pdf”, media_type=”application/pdf”, … Read more

How to download a file using ReactJS with Axios in the frontend and FastAPI in the backend?

In the Axios GET request, you have to make sure the responseType parameter is set to blob. Once you get the response from the API, you will need to pass the Blob object (i.e., response.data) to the URL.createObjectURL() function. Below is a fully working example on how to create and download a file (Document), using … Read more

How to return and download Excel file using FastAPI?

You could set the Content-Disposition header using the attachment parameter, indicating to the browser that the file should be downloaded, as described in the answers here and here. Swagger UI will provide a Download file link for you to download the file, as soon as you execute the request. headers = {‘Content-Disposition’: ‘attachment; filename=”Book.xlsx”‘} return … Read more

How to send a base64 encoded image to a FastAPI backend?

As described in this answer, uploaded files are sent as form data. As per FastAPI documentation: Data from forms is normally encoded using the “media type” application/x-www-form-urlencoded when it doesn’t include files. But when the form includes files, it is encoded as multipart/form-data. If you use File, FastAPI will know it has to get the … Read more

Cannot Upload File to FastAPI backend using JavaScript Fetch API in the frontend

As per the documentation: Warning: When using FormData to submit POST requests using XMLHttpRequest or the Fetch_API with the multipart/form-data Content-Type (e.g. when uploading Files and Blobs to the server), do not explicitly set the Content-Type header on the request. Doing so will prevent the browser from being able to set the Content-Type header with … Read more