The issue was with the axios instance.

I have added response type to the request header, the issue was solved.

        responseType: "blob",

this was the root cause. After that I was able to download the file with response provided by 'Klian' in the comments to my question.

Answer from Nabeel Mhd on Stack Overflow
🌐
Reddit
reddit.com › r/nextjs › is there a 'best practice' for downloading files from an api response?
r/nextjs on Reddit: Is there a 'best practice' for downloading files from an API response?
November 6, 2023 -

I'm working with an API res that contains a zip, csv, and pdf. I've been researching how I should download and/or create download buttons for these files but a lot of articles I'm looking through specify that their method is 'hacky' (or something similar).

I don't like the idea of pushing a 'hacky' solution to production. It may be a starting point but not really a good foundation to build off of. Does anybody know of good practices for this kind of procedure?

I've boiled it down to:

  1. Parse the response

  2. Store the file contents

  3. Create sources for the downloads

but surely it isn't that simple, right?

🌐
Medium
medium.com › @khushbooverma8319 › download-api-file-in-frontend-91bd51e4ee19
Download file in frontend getting from API | by Khushbooverma | Medium
May 22, 2023 - <a href={serviceURL} download="file.csv"> // url getting from the api Download </a> ... Suppose we have a button on click of which we want to download the CSV file. We have a onClick handler named handlDownload as shown below. const handleDownload ...
🌐
OutSystems
outsystems.com › forums › discussion › 83297 › download-file-from-api-response
Download File from API Response | OutSystems
How can we download a file from API response below is the response body which I couldnt convert and download. File type might be anything .xlsx, .PDF, .docx, .txt · With this resposne I'm able to download the actual file from Postman, but not in outsystems flow.
🌐
Reddit
reddit.com › r/webdev › download file from api response
r/webdev on Reddit: Download file from API response
November 22, 2024 -

Apologies if I am not using the right terms to explain.

I am building some public API endpoints which will request data from another service. The response is a stream of data. How can I trigger download action (since there is no client side js in work, I doubt I can use any HTML tags to do it.)

Tried setting the disposition header but it did not help either.
I believe I should store this stream response somewhere like s3 or some cloud storage and trigger the download from there or can I send a zip file as a response?

EDIT: The disposition header worked with a get call. Thanks for helping out.

It also works with post I think but I will test that at a later point.

🌐
CodeSignal
codesignal.com › learn › courses › efficient-api-interactions-with-java › lessons › downloading-files-from-an-api-using-javas-httpclient
Downloading Files from an API Using Java's HttpClient
When you send a GET request using Java's HttpClient, your client communicates with the server at a specified URL, asking it to provide the file. The server responds with the file data, if available and permissible, along with an HTTP status code (like 200 OK). Here's a basic example of downloading a file named welcome.txt from our API at http://localhost:8000/notes. This approach downloads the entire file at once, which is manageable for smaller files. This code sends a GET request and writes the response content to a local file.
🌐
CodeSignal
codesignal.com › learn › courses › efficient-api-interactions-with-javascript › lessons › downloading-files-from-an-api-using-javascript
Downloading Files from an API Using JavaScript
This code sends a GET request and writes the full response content to a local file. This method works well for small files but can strain memory for larger files. When dealing with large files, downloading them all at once can be inefficient and strain memory. To address this, you can use the ReadableStream API to handle streaming.
Find elsewhere
🌐
Oracle
docs.oracle.com › en › cloud › paas › content-cloud › rest-api-documents › op-documents-api-1.2-files-fileid-data-get.html
REST API for Documents - Download File
November 27, 2023 - The following example downloads version 1 of the specified file. If the status code indicates success (200), the response includes a data stream that contains the file contents. The example uses a public link ID because this file is under a folder structure not owned by or shared with the current user.
🌐
UiPath Community
forum.uipath.com › help › studio
How to download a file through API after the initial binary response? Formstack - Studio - UiPath Community Forum
July 26, 2022 - Hi, These are the API details I am working through - /download/:submission_id/:field_id I am trying to download a file through API, but the API call results in binary encoding of the file. The link above says - “Download previously uploaded file via submission.
🌐
CodeBurst
codeburst.io › download-files-using-web-api-ae1d1025f0a9
Download Files using Web API. How to return a file from an API… | by Changhui Xu | codeburst
February 24, 2021 - In this article, I will use a demo ... HTML page, end users can left-click a hyperlink to download the file or right-click the link to choose “Save Link As” in the context menu and save the file....
🌐
Retool
community.retool.com › 💬 app building
Downloading files retrieved via API - 💬 App Building - Retool Forum
July 18, 2024 - Goal: Trying to download files pulled from an API, but they keep downloading corrupt Steps: utils.downloadFile(DownloadFile2.data, 'test', 'pdf') - have tried adding {base64Binary: } have tried modifying the API headers a few ways. Currently using content type: application/octet-stream and using this transform const base64Message = "data:application/pdf;base64" + btoa(unescape(encodeURIComponent(data.message))) return base64Message; That seems to get good base64, but the download stay...
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › 809ff52b-c796-4734-a2a4-cebe56b16bdb
How to download File from api where api sends stream of file using cross file download manager? | Microsoft Learn
You could try to use the following code to download file: HttpClient client = new HttpClient(); var uri = new Uri("http://xxx.xxx.xxx/api/Values?fileName=abc.txt"); var response = await client.GetAsync(uri); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStreamAsync(); } If anyone knows to send the file instead of its stream from API then please help!
🌐
Sugarsync
sugarsync.com › dev › download-file-example.html
SugarSync for Developers-API Examples: Downloading a File
The response should look something like this: HTTP/1.1 200 OK Content-Type: application/octet-stream; charset=UTF-8 Content-Length: 18241 Date: Mon, 05 Dec 2011 19:04:51 GMT Accept-Ranges: bytes Content-Disposition: attachment; filename*=UTF-8''Golden Gate Bridge.jpg Access-Control-Allow-Origin: * Server: Noelios-Restlet-Engine/1.1.5 · After an app downloads a file, it can then display the file to the user.
🌐
Zoom Developer Forum
devforum.zoom.us › zoom for government
How to get the file with the API response download_url - Zoom for Government - Zoom Developer Forum
February 7, 2023 - The following API response includes messages\files\download_url field. Could you help clarify how to get the file with this download_url? https://marketplace.zoomgov.com/docs/api-reference/chat/methods/#operation/getCha…
🌐
CodeSignal
codesignal.com › learn › courses › efficient-api-interactions-with-scala › lessons › downloading-files-from-an-api-using-requests-scala
Downloading Files from an API | CodeSignal Learn
These notes can be downloaded or uploaded through the /notes endpoint, allowing functionality for storing supplementary information. For example, users might keep notes about a meeting or important reminders. By understanding how to interact with this endpoint, you can effectively manage notes within your application. By the end of this lesson, you'll know how to request a file from an API, save it locally, and verify its contents.
🌐
Mendix
community.mendix.com › link › spaces › app-development › questions › 106006
Download file via rest api
March 26, 2021 - The Mendix Forum is the place where you can connect with Makers like you, get answers to your questions and post ideas for our product managers.
🌐
Royal Jay
royaljay.com › home › blog › how to download a file from an authenticated web api endpoint
How to Download Files from Authenticated Web API End Points
April 6, 2017 - In this tutorial, I’m going to show you how to use Angular to download a file from an authenticated ASP.NET Web API endpoint.