how to get response as blob
I am facing issue with responseType blob. I don't see any workaround to get receive response as Blob.
My OpenAPI specification as follows:
and my actual code to download the file is below.
but somehow I am getting error while opening file. Error is content is corrupted. I did all approach to set as blob but it's not working.
Please give suggestion.
@sachinpatil2 your problem is that every response that is not application/json get treated as text. See here:
https://github.com/ferdikoomen/openapi-typescript-codegen/blob/8379bfeb3201c41b1462f42f3b82bb54fda3bd5a/src/templates/core/fetch/getResponseBody.hbs#L8-L12
So your blob get converted to text and then its not a valid xlsx anymore
Currently having the same problem
so don't we have solution on this as of today? @MadddinTribleD
Looks like a no (or no not yet) to me.
There are several issues with binary data already:
- https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1559
- https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1445
- https://github.com/ferdikoomen/openapi-typescript-codegen/issues/981
and also PRs that implements binary responses https://github.com/ferdikoomen/openapi-typescript-codegen/pull/1043 https://github.com/ferdikoomen/openapi-typescript-codegen/pull/986
Thanks for your prompt response. appreciate it. @MadddinTribleD
Would it be a good idea to have an options argument, that lets me retrieve the raw response?
like:
const response = await generatedAPIFunction({param1: "foo", param2: "bar"}, {response: "raw"});
// now I can do whatever I want with it:
const blob = await response.blob();
``
I edited
in
const isBlob = contentType.match(/pdf|octet-stream/)
if (isJSON) {
return await response.json();
}else if (isBlob){
return await response.blob();
} else {
return await response.text();
}
so application/pdf and octet-stream would return a blob. It's weird that something like this wasn't already implemented, as the service correctly determined that I wanted Blob as the CancelablePromise type when it read my openapi configuration
Thanks for raising this issue.
According to the OpenAPI v3.0 docs, it could be easily detected by the format: binary property:
paths:
/report:
get:
summary: Returns the report in the PDF format
responses:
'200':
description: A PDF file
content:
application/pdf:
schema:
type: string
format: binary
I would really love if this format: binary property would make axios-dependent generated client to contain
responseType: "blob".
My actual workaround is to use my own request.ts file and detect by url if it's blob or not.
Check out our fork @hey-api/openapi-ts. This issue is fixed there. If you run into anything further issues open a issue there and we will look into fixing it!