Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify OpenAPI for binary download in angular

I have a method to download a binary file. This currently has the following spec in openapi.yaml:

  /processing/getZippedReports:
    post:
      tags:
      - ProcessingRest
      description: Get the reports
      operationId: getZippedReports
      requestBody:
        description: The list of items for which to get the HTML
        content:
          application/json:
            schema:
              type: array
              items:
                type: string
        required: true
      responses:
        200:
          description: file with zipped reports
          content:
            application/octet-stream: {}

The generated typescript-angular code contains this call to httpClient:

return this.httpClient.post<any>(`${this.configuration.basePath}/processing/getZippedReports`,
    requestBody,
    {
        withCredentials: this.configuration.withCredentials,
        headers: headers,
        observe: observe,
        reportProgress: reportProgress
    }
);

This way, angular seems unable to receive the binary content. I had to change the httpClient, use the non-type-variable signature of post and add responseType: blob to the parameters

return this.httpClient.post(`${this.configuration.basePath}/processing/getZippedReports`,
    requestBody,
    {
        withCredentials: this.configuration.withCredentials,
        headers: headers,
        observe: 'response',
        responseType: 'blob',
        reportProgress: reportProgress
    }
);

Is this a bug / missing feature in Swagger codegen or should I change the openapi definition to get working angular code?

like image 983
Alex R Avatar asked Mar 05 '26 17:03

Alex R


1 Answers

To indicate that the response is a binary file, use a schema with type: string with format: binary.

          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
like image 197
Helen Avatar answered Mar 07 '26 10:03

Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!