首页 > 解决方案 > 使用 NodeJS Loopback 调用其他 API 后如何查看标头?

问题描述

发送请求后,看不到响应的标头

  1. 在控制器中,我有发送请求的方法:
     @get('/getToken')
              async getToken(): Promise<any> {
                return await this.s4hanapurreqService.getToken(); <- Response Ok
              }
  1. 此方法在服务文件中调用另一个:

界面:

    export interface S4hanapurreqService {
    getToken(): Promise<any>;
    }
  1. 该接口 S4hanapurreqService 与数据源通信
      async value(): Promise<S4hanapurreqService> {
        return getService(this.dataSource);
      }

数据源:

     "operations": [
        {
          "template": {
            "method": "GET",
            "url": "https://<some url>",
            "headers": {
              "x-csrf-token": "fetch"
            }
          },
          "functions": {
            "getToken": [] <-- this is my controller method
          }
        }

一切正常,因为我得到了回应:

控制器:

    @get('/getToken')
      async getToken(): Promise<any> {
        return await this.s4hanapurreqService.getToken(); <- Response Ok
      }

json响应:

    {
        "d": {
            "EntitySets": [
                 ... Data  ...
            ]
        }
    }

但是当我调试时,我发现我只有这个,并且标题看起来不像状态 ,200,201 等

我不能使用这个因为失败:

    var token = response.headers['x-crsf-token'];

从我的响应中获取标题的正确代码是什么?

问候。

标签: node.jshttpresponseloopbackjsloopback

解决方案


推荐阅读