Created
May 26, 2024 12:18
-
-
Save eyalcohen4/488cc46b5a63751ac1fe668d00d22c2b to your computer and use it in GitHub Desktop.
ndjson-with-axios.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import { parse } from 'ndjson'; | |
async function fetchNDJSON(url: string): Promise<void> { | |
try { | |
const response = await axios({ | |
method: 'get', | |
url: url, | |
responseType: 'stream' | |
}); | |
const stream = response.data; | |
stream.pipe(parse()) | |
.on('data', (obj: any) => { | |
console.log('Parsed JSON object:', obj); | |
}) | |
.on('error', (error: any) => { | |
console.error('Error parsing NDJSON:', error); | |
}); | |
} catch (error) { | |
console.error('Error fetching NDJSON:', error); | |
} | |
} | |
fetchNDJSON('https://example.com/ndjson-endpoint'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment