Window Fetch Response can't be converted to JSON when URL object passed as parameter

73 Views Asked by At

According to the documentation of the Fetch method , it accepts a:

resource

This defines the resource that you wish to fetch. This can either be:

A string or any other object with a stringifier — including a URL object — that provides the URL of the resource you want to fetch.

A Request object.

But when I construct a URL the response body is stream: undefined:

  const url = new URL(
    '/traffic',
    'https://some-base-domain.com'
  )
  const [err, res] = await to(fetch(url))

  if (err) {
    console.error(err)
    return
  }
  const data = await res.json()
  return data

Response:

Response {
  [Symbol(realm)]: null,
  [Symbol(state)]: {
    aborted: false,
    rangeRequested: false,
    timingAllowPassed: true,
    requestIncludesCredentials: true,
    type: 'default',
    status: 404,
    timingInfo: {
      startTime: 5089772.463499999,
      redirectStartTime: 0,
      redirectEndTime: 0,
      postRedirectStartTime: 5089772.463499999,
      finalServiceWorkerStartTime: 0,
      finalNetworkResponseStartTime: 0,
      finalNetworkRequestStartTime: 0,
      endTime: 0,
      encodedBodySize: 431,
      decodedBodySize: 431,
      finalConnectionTimingInfo: null
    },
    cacheState: '',
    statusText: '',
    headersList: HeadersList {
      cookies: null,
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: null
    },
    urlList: [ [URL] ],
    body: { stream: undefined }
  },
  [Symbol(headers)]: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(5) {
      'date' => [Object],
      'content-type' => [Object],
      'content-length' => [Object],
      'connection' => [Object],
      'content-language' => [Object]
    },
    [Symbol(headers map sorted)]: null
  }
}
0

There are 0 best solutions below