I have this cURL request: curl -X GET \…
- User
I have this cURL request: curl -X GET
https://api.rev.com/api/v1/attachments/6SWCOBmRAwAAAAAA/content.srt
-H 'Authorization: Rev [ClientApiKey]:[UserAPIKey]' It’s also possible to download the file rather than retrieve the data. You can do so cURL using -o. But I need to download file using this node js code: var https = require('https');var options = { host: 'api.rev.com', path: '/api/v1/attachments/6SWCOBmRAwAAAAAA/content.srt', method: 'GET', headers: { 'Authorization': 'Rev [ClientApiKey]:[UserAPIKey]' } };
var req = https.request(options, (resp) => { var data = [];
resp.on('data', (chunk) => { data.push(chunk); });
resp.on('end', function() { console.log('Status Code:', resp.statusCode); console.log(JSON.parse(data)); }); }); req.end();
- Assistant
To download a file using Node.js, you can extend your current example by writing the data chunks directly to a file instead of storing them in memory. You can use the Node.js
fs(filesystem) module to handle file writing. Here is a modified version of your code that will download the file and save it:const https = require('https'); const fs = require('fs'); const options = { host: 'api.rev.com', path: '/api/v1/attachments/6SWCOBmRAwAAAAAA/content.srt', method: 'GET', headers: { 'Authorization': 'Rev [ClientApiKey]:[UserAPIKey]' } }; const file = fs.createWriteStream("content.srt"); const req = https.request(options, (resp) => { if (resp.statusCode !== 200) { console.error(`Failed to get 'content.srt'. Status Code: ${resp.statusCode}`); return; } resp.pipe(file); file.on('finish', () => { file.close(() => { console.log('Download complete.'); }); }); }); req.on('error', (e) => { console.error(`Problem with request: ${e.message}`); }); req.end(); - User
air-trofim:conversation_bot trol$ npm start
conversation_bot@1.0.0 start node ./src/main.js
file:///Users/trol/Programs/conversation_bot/src/rev_api/revAPI.js:2 import {fs} from 'fs' ^^ SyntaxError: The requested module 'fs' does not provide an export named 'fs' at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21) at async ModuleJob.run (node:internal/modules/esm/module_job:214:5) at async ModuleLoader.import (node:internal/modules/esm/loader:329:24) at async loadESM (node:internal/process/esm_loader:28:7) at async handleMainPromise (node:internal/modules/run_main:113:12)
- Assistant