Angular 8 HttpClient GET忽略了来自swagger API的responseType:'blob',返回{}

问题描述 投票:0回答:1

像这样在swagger 2.0中指定API服务:

 /example/my-file:
    get:
      summary: Get this file
      tags:
        - MyFile
      produces:
        - application/octet-stream
      responses:
        200:
          description: OK
          schema:
            type: file
        400:
          $ref: "#/responses/badRequest"
        404:
          $ref: "#/responses/notFound"
        500:
          $ref: "#/responses/internalServerError"

已实现,并且可以发出可以正确返回文件内容的Postman GET请求。我使用ng-swagger-gen生成了角度服务调用:

getExampleFileResponse(params: ExampleService.GetExampleFileParams): __Observable<__StrictHttpResponse<Blob>> {
    let __params = this.newParams();
    let __headers = new HttpHeaders();
    let __body: any = null;


    let req = new HttpRequest<any>(
      'GET',
      this.rootUrl + `/example/my-file`,
      __body,
      {
        headers: __headers,
        params: __params,
        responseType: 'blob'
      });

    return this.http.request<any>(req).pipe(
      __filter(_r => _r instanceof HttpResponse),
      __map((_r) => {
        return _r as __StrictHttpResponse<Blob>;
      })
    );
  }


  getExampleFile(params: ExampleService.GetExampleFileParams): __Observable<Blob> {
    return this.getExampleFileResponse(params).pipe(
      tap(_r => console.log(_r)),
      __map(_r => _r.body as Blob)
    );
  }

当我从有角度的客户打来电话时:

   this.ExampleService.getExampleFile(param)
    .subscribe((f) => {
      saveAs(f, 'example.txt')
    });

我得到一个由3个字节组成的Blob:{} \ n不是文件内容。似乎服务中的某些内容无法正常工作。如果我将tap()放入响应中的服务,我可以看到响应正在传递具有这三个字节和内容类型的Blob:application / json,而不是我在API中指定的application / octet-stream。] >

在swagger 2.0中指定了API服务,如下所示:/ example / my-file:get:summary:获取此文件标签:-MyFile产生:-application / octet-stream ...

angular blob content-type
1个回答
0
投票
确定。缺少信息:我们正在使用拦截器注入auth令牌。我们需要添加并接受:application / json,application / octet-stream,

/

© www.soinside.com 2019 - 2024. All rights reserved.