angular - 如何指定 OpenAPI 以 angular 进行二进制下载

标签 angular swagger-codegen

我有一种下载二进制文件的方法。目前在 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: {}

生成的 typescript-angular 代码包含对 httpClient 的调用:
return this.httpClient.post<any>(`${this.configuration.basePath}/processing/getZippedReports`,
    requestBody,
    {
        withCredentials: this.configuration.withCredentials,
        headers: headers,
        observe: observe,
        reportProgress: reportProgress
    }
);

这样,angular 似乎无法接收二进制内容。我不得不更改 httpClient,使用 post 的非类型变量签名并添加 responseType: blob到参数
return this.httpClient.post(`${this.configuration.basePath}/processing/getZippedReports`,
    requestBody,
    {
        withCredentials: this.configuration.withCredentials,
        headers: headers,
        observe: 'response',
        responseType: 'blob',
        reportProgress: reportProgress
    }
);

这是 Swagger 代码生成中的错误/缺失功能还是我应该更改 openapi 定义以获得工作的 Angular 代码?

最佳答案

要指示响应是二进制文件,请使用带有 type: string 的模式与 format: binary .

          content:
            application/octet-stream:
              schema:
                type: string
                format: binary

关于angular - 如何指定 OpenAPI 以 angular 进行二进制下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53309421/

相关文章:

spring-boot - 更新 Swagger code-gen 生成的代码

javascript - 在 Angular 中使用什么更好 - src 或 [src]

javascript - Angular2 routerLink 参数

spring - 如何更改 open api 代码生成器输入 yml 文件中的打包结构

gradle - 如何配置 micronaut 序列化以使用开放 api 生成的模型?

java - Swagger codegen RX JAVA + 改造不起作用

eclipse - 找不到处理 swagger-codegen-maven-plugin 的市场条目

javascript - Angular 2 : render a component without its wrapping tag

angular - 什么将两个组成部分定义为家庭?

javascript - handsontable - 刷新数据并维护过滤和排序条件