0

I'm trying to consume an API endpoint that returns data in ndjson form. I'm using the can-ndjson-stream and undici packages. When I try to hit the endpoint, a status code of 200 is returned, but the status in Chrome's developer tools says, "Pending". Also, what looks like an empty response if type BodyReadable is sent. After 20 or so seconds, an ECONNRESET error is thrown.

 getStream = (url, toke, params) =>
    new Promise<T>(async (resolve, reject) => {
      try {
        const { body, headers, statusCode, trailers } = await request(url + params, {
          headers: {
            Accept: 'application/x-ndjson',
            Authorization: `Bearer ${token}`
          }
        });
        console.log(`StatusCode ${statusCode}`)
        console.log('Body')
        console.log(body)
        const response = []
        for await (const data of body) {
          console.log(`Data: ${data}`)
          response.push(data)
        }
      } catch(e) {
        console.log(`Error  ${e}`)
        reject(e)
      }
    });

The for loop in the try block doesn't appear to get executed at all. I know the code snippet is incomplete; I'm not calling ndjson.parse() or actually resolving the promise, for example. But I have to get past this first. This endpoint is developed by another team, so I don't have access to its internals. I suspect there's something more I should be doing to read data from the stream, but I'm at a loss. Does anyone know what I have to do to consume data?

TIA!

boing
  • 499
  • 2
  • 6
  • 22
  • What library is `request()`? Does it support promises? Is it reading the body of the response or just the headers? – jfriend00 Apr 12 '23 at 18:42

0 Answers0