openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

how to get response as blob

Open sachinpatil2 opened this issue 2 years ago • 8 comments

I am facing issue with responseType blob. I don't see any workaround to get receive response as Blob.

My OpenAPI specification as follows: image

and my actual code to download the file is below. image

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 avatar Sep 01 '23 11:09 sachinpatil2

@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

MadddinTribleD avatar Sep 11 '23 13:09 MadddinTribleD

so don't we have solution on this as of today? @MadddinTribleD

sachinpatil2 avatar Sep 11 '23 13:09 sachinpatil2

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

MadddinTribleD avatar Sep 11 '23 13:09 MadddinTribleD

Thanks for your prompt response. appreciate it. @MadddinTribleD

sachinpatil2 avatar Sep 11 '23 14:09 sachinpatil2

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();
``

manuschillernordlb avatar Sep 18 '23 12:09 manuschillernordlb

I edited

in /core/requests.ts getResponseBody

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

nickveliki avatar Nov 06 '23 10:11 nickveliki

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.

Mikrovolnovka avatar Jan 17 '24 17:01 Mikrovolnovka

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!

jordanshatford avatar Apr 02 '24 10:04 jordanshatford