I am trying to download file by calling external API in nestjs.
Below is my code in service
import { Injectable } from '@nestjs/common';
import * as fs from "fs";
import * as axios from "axios";
@Injectable()
export class FileService {
saveFile() {
return axios.default
.get("https://www.nseindia.com/")
.then((res) => {
return axios.default.get(
"https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY",
{
headers: {
cookie: res.headers["set-cookie"],
},
}
);
})
.then((res) => {
//console.log(res.data);
let data = JSON.stringify(res.data);
fs.writeFileSync("../files/option-chain-indices.json", data);
return data;
})
.catch((err) => {
console.log(err);
});
}
}
I am getting error as below
error TS2322: Type 'string[]' is not assignable to type 'string'.
cookie: res.headers['set-cookie']
how can I solve this problem?
>Solution :
Can you change below line of code as follows?
cookie: res.headers["set-cookie"] --> cookie: res.headers["set-cookie"].join(';')