【问题标题】:Get all assets from RefTab API (Format RefTab API Response)从 RefTab API 获取所有资产(格式化 RefTab API 响应)
【发布时间】:2023-04-05 08:40:01
【问题描述】:

我正在尝试使用 RefTab 的 API 来提取自定义报告,但我不确定如何从响应中提取我想要的数据。

我认为这可能与解压缩gunzip有关?

我已经尝试访问 res.headers 和 res.body,但这并不是我想要的。

我似乎在 RefTab 上的任何地方都找不到任何东西。 我没有使用 REST API 的经验,所以我不知道这是否特定于 RefTab

任何帮助将不胜感激!

https://reftab.com

https://reftab.com/api-docs

我的 index.js 文件:

const btoa = require('btoa');
const fetch = require('node-fetch');
const CryptoJS = require('crypto-js');

//CryptoJS is needed for the md5 and HmacSHA256 methods
function signRequest(request) {
  const publicKey = 'PUBLIC_KEY_HERE';
  const secretKey = 'SECRET_KEY_HERE';
  const body = request.body;
  const method = request.method;
  const url = request.url;
  const now = new Date().toUTCString();
  let contentMD5 = '';
  let contentType = '';
  if (body !== undefined) {
    contentMD5 = CryptoJS.md5(body).toString();
    contentType = 'application/json';
  }
  let signatureToSign = method + '\n' +
    contentMD5 + '\n' +
    contentType + '\n' +
    now + '\n' +
    url;
  signatureToSign = unescape(encodeURIComponent(signatureToSign));
  const token = btoa(CryptoJS.HmacSHA256(signatureToSign, secretKey));
  const signature = 'RT ' + publicKey + ':' + token;
  request.headers = {};
  request.headers.Authorization = signature;
  request.headers['x-rt-date'] = now;
  return request;
}

//expected input object
const options = {
  method: 'GET',
  url: 'https://www.reftab.com/api/assets'
};
fetch('https://www.reftab.com/api/assets', signRequest(options))
  .then((res) => {
    console.log(res);
  });

回应:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: Gunzip {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: true,
      allowHalfOpen: true,
      _transformState: [Object],
      _hadError: false,
      bytesWritten: 0,
      _handle: [Zlib],
      _outBuffer: <Buffer a0 ac 36 be 49 02 00 00 e0 46 33 be 49 02 00 00 6e 5b b1 6b 01 09 00 00 8c 21 00 00 36 09 00 00 30 87 34 be 49 02 00 00 f0 7d 34 be 49 02 00 00 62 69 ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 2,
      _finishFlushFlag: 2,
      _defaultFullFlushFlag: 3,
      _info: undefined,
      _level: -1,
      _strategy: 0,
      [Symbol(kCapture)]: false
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://www.reftab.com/api/assets',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

【问题讨论】:

    标签: node.js json api rest gunzip


    【解决方案1】:

    我变了:

    fetch('https://www.reftab.com/api/assets', signRequest(options))
      .then((res) => {
        console.log(res);
      });
    

    收件人:

    fetch('https://www.reftab.com/api/assets', signRequest(options))
      .then((res) => res.json())
      .then(json => console.log(json))
    

    它成功了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2022-10-04
      • 2015-02-14
      • 1970-01-01
      相关资源
      最近更新 更多