knuckleswtf / knuckleswtf/scribe

Download binary response file from Laravel API

Open
#900 3 comments 0 reactions 0 assignees View on GitHub
feature-request help wanted validated
Dominant language
PHP
Stars
2.3k
Forks
357
PR merge metrics
No merged PRs in 30d

Description

### Scribe version

4.37

### Your question

Hello, I just wanted to know if there is any way to download a zip file that comes in binary format and is obtained from a Laravel API response using response()->download().

I have tried doing it with FileSaver and transforming it to a Blob, but the downloaded files are corrupted.

```
return preflightPromise.then(() => makeAPICall(method, path, body, query, headers, endpointId))
.then(([responseStatus, statusText, responseContent, responseHeaders]) => {
responsePanel.hidden = false;
responsePanel.querySelector(`.response-status`).textContent = responseStatus + " " + statusText ;

let contentEl = responsePanel.querySelector(`.response-content`);
if (responseContent === '') {
contentEl.textContent = contentEl.dataset.emptyResponseText;
return;
}

const blob = new Blob([responseContent], { type: 'application/zip' });
saveAs(blob, 'file.zip');

// Prettify it if it's JSON
let isJson = false;
try {
const jsonParsed = JSON.parse(responseContent);
if (jsonParsed !== null) {
isJson = true;
responseContent = JSON.stringify(jsonParsed, null, 4);
}
} catch (e) {}

contentEl.innerHTML = responseContent;
isJson && window.hljs.highlightElement(contentEl);
})
.catch(err => {
console.log(err);
let errorMessage = err.message || err;
errorPanel.hidden = false;
errorPanel.querySelector(`.error-message`).textContent = errorMessage;
})
.finally(() => { btnElement.disabled = false } );
```

In Swagger there's an option that creates a link to download the file, and it works correctly.

Thank you and best regards.

### Docs

- [X] I've checked [the docs](https://scribe.knuckles.wtf/laravel), the [troubleshooting guide](https://scribe.knuckles.wtf/laravel/troubleshooting), and [existing issues](https://github.com/knuckleswtf/scribe/issues?q=is%3Aissue+), but I didn't find a solution

Contributor guide

Open the contributing guide

Research direction

Start by tracing the response handling shown in the issue, especially makeAPICall and the Blob/FileSaver download path, and compare it with the working Swagger behavior. Done means a binary ZIP returned by Laravel's response()->download() can be downloaded from the generated Scribe documentation without corruption.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, laravel, php
Domain
documentation, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.