Home > Back-end >  Why does my response look like this? Node-Fetch
Why does my response look like this? Node-Fetch

Time:03-31

I'm a newbie to Node JS and I'm trying to get the data from this URL: https://www.tiktok.com/node/share/user/@test/

You don't need to sign into anything to look at the data, you can literally click the link and you see the data I want to access. However, when I try to use node-fetch to retrieve this data so I can use it, I get a weird response (or, in better terms, I don't know what it means lol)

Response {
  size: 0,
  [Symbol(Body internals)]: {
    body: BrotliDecompress {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      bytesWritten: 0,
      _handle: [BrotliDecoder],
      _outBuffer: <Buffer 80 71 b8 8d 94 7f 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 50 29 84 02 00 00 00 00 90 23 84 02 00 00 00 00 00 00 ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 0,
      _finishFlushFlag: 2,
      _defaultFullFlushFlag: 1,
      _info: undefined,
      _maxOutputLength: 4294967296,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: null,
      [Symbol(kError)]: null
    },
    stream: BrotliDecompress {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      bytesWritten: 0,
      _handle: [BrotliDecoder],
      _outBuffer: <Buffer 80 71 b8 8d 94 7f 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 50 29 84 02 00 00 00 00 90 23 84 02 00 00 00 00 00 00 ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 0,
      _finishFlushFlag: 2,
      _defaultFullFlushFlag: 1,
      _info: undefined,
      _maxOutputLength: 4294967296,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: null,
      [Symbol(kError)]: null
    },
    boundary: null,
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    type: 'default',
    url: 'https://www.tiktok.com/node/share/user/@test',
    status: 200,
    statusText: 'OK',
    headers: {
      'cache-control': 'max-age=0, no-cache, no-store',
      connection: 'close',
      'content-encoding': 'br',
      'content-length': '991',
      'content-type': 'application/json; charset=utf-8',
      date: 'Wed, 30 Mar 2022 20:20:29 GMT',
      expires: 'Wed, 30 Mar 2022 20:20:29 GMT',
      pragma: 'no-cache',
      'referrer-policy': 'strict-origin-when-cross-origin',
      server: 'nginx',
      'server-timing': [Array],
      'set-cookie': [Array],
      'strict-transport-security': 'max-age=31536000; includeSubDomains',
      'x-akamai-request-id': '3784d080.1aa6322a',
      'x-cache': 'TCP_MISS from a23-48-94-141.deploy.akamaitechnologies.com (AkamaiGHost/10.7.3.1-40349883) (-)',
      'x-cache-remote': 'TCP_MISS from a23-15-9-28.deploy.akamaitechnologies.com (AkamaiGHost/10.7.3.1-40349883) (-)',
      'x-content-type-options': 'nosniff',
      'x-download-options': 'noopen',
      'x-frame-options': 'SAMEORIGIN',
      'x-ms-token': 'jtKW5E_FJyaGwbk6hof4wjMp-LxmnCeL3iIVLSNu5BjJXmX16YSt-nHq63kbAL0QIiIf43Kmi9EjXgk2Qvd2K2aaz7p91th3qsRD',
      'x-origin-response-time': '189,23.15.9.28',
      'x-parent-response-time': '209,23.48.94.141',
      'x-tt-logid': '202203302020280101130060380B3C7E54',
      'x-tt-trace-host': '013d901111266bc0a1c59117d43662ef4d189a61edb84152e27f18c97ac8160a34e528b75987e12883be0879504e89313478ad3ad6007087782ae6295db5253d379511105161d49f40cc7c706ee0e3592a4ccaeeb591ac76187f951abae0ae5c7b',
      'x-tt-trace-tag': 'id=16;cdn-cache=miss;type=dyn',
      'x-ua-compatible': 'IE=edge,chrome=1',
      'x-xss-protection': '1; mode=block'
    },
    counter: 0,
    highWaterMark: 16384
  }
}

This is the code I used.

const fetch = require('node-fetch')

fetch("https://www.tiktok.com/node/share/user/@test/")
.then(res => {
  console.log(res);
})

I looked through this whole thing and can't quite understand what it means. Sorry for asking but yeah

CodePudding user response:

What you're looking at is the Response object which represents the entire HTTP response. If you're only interested in the JSON data, you can extract it using the Response.json() method:

const fetch = require("node-fetch");

// you'll get an "invalid json response body" error without this
const headers = { "User-Agent": "node-fetch" };

const response = fetch("https://www.tiktok.com/node/share/user/@test", {
  headers,
})
  // first, extract JSON data from `Response` object
  .then((res) => res.json())
  // then print JSON data that was parsed
  .then((data) => console.log(data));
  • Related