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

Ale*_*x R 3 swagger-codegen angular

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

生成的 typescript-angular 代码包含对 httpClient 的调用:

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

这样,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
    }
);
Run Code Online (Sandbox Code Playgroud)

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

Hel*_*len 6

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

          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
Run Code Online (Sandbox Code Playgroud)

  • 我遇到了与原始海报相同的问题。我已经尝试过这个@Helen,因为它被标记为已接受的答案,但在生成的代码中仍然不包含 `responseType: 'blob'` 。有什么帮助吗? (2认同)