I currently have a Nestjs server setup and am attempting to perform an Axios request when one of the endpoints is hit with a GET request. Here is the controller.ts code:
@Controller()
export class TestController {
constructor(private readonly testService: TestService) {}
@Get('testData')
testData() {
return this.testService.testData();
}
}
Service.ts:
@Injectable()
export class TestService {
status(): string {
return 'OK'
}
testData(): Promise<any> {
return helper.getTestData();
}
}
Where helper.getTestData()
is just a call to a helper file with the following function:
export async function getTestData(): Promise<any> {
const result = await axios({
url: tempURL,
method: 'GET',
timeout: 3000,
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
I am able to hit this endpoint tempURL
but encounter the following error message: Cannot read property 'Agent' of undefined
. I know that the endpoint I am attempting to hit requires a cert, which is why I must include the httpsAgent argument inside the Axios request. If I don't include the httpsAgent
argument, I receive the following message Error: unable to verify the first certificate in nodejs
.
Is there a way to configure Nestjs to work with https? Or is there another way to handle this authorization issue inside of Nestjs? Using Postman everything works fine so I'm assuming it is a Nestjs issue. Any help is appreciated.
I currently have a Nestjs server setup and am attempting to perform an Axios request when one of the endpoints is hit with a GET request. Here is the controller.ts code:
@Controller()
export class TestController {
constructor(private readonly testService: TestService) {}
@Get('testData')
testData() {
return this.testService.testData();
}
}
Service.ts:
@Injectable()
export class TestService {
status(): string {
return 'OK'
}
testData(): Promise<any> {
return helper.getTestData();
}
}
Where helper.getTestData()
is just a call to a helper file with the following function:
export async function getTestData(): Promise<any> {
const result = await axios({
url: tempURL,
method: 'GET',
timeout: 3000,
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
I am able to hit this endpoint tempURL
but encounter the following error message: Cannot read property 'Agent' of undefined
. I know that the endpoint I am attempting to hit requires a cert, which is why I must include the httpsAgent argument inside the Axios request. If I don't include the httpsAgent
argument, I receive the following message Error: unable to verify the first certificate in nodejs
.
Is there a way to configure Nestjs to work with https? Or is there another way to handle this authorization issue inside of Nestjs? Using Postman everything works fine so I'm assuming it is a Nestjs issue. Any help is appreciated.
Share Improve this question asked Jul 3, 2021 at 21:03 AndrewAndrew 5014 silver badges13 bronze badges 5-
looks like
https
isundefined
somehow – Micael Levi Commented Jul 3, 2021 at 21:42 - @MicaelLevi https is defined, I have it properly imported into the file. Also, https is a native module, how could it be undefined? – Andrew Commented Jul 3, 2021 at 23:29
-
1
depending on how you're importing it, it could. The line in the stack trace that the error
Cannot read property 'Agent' of undefined
appears is the same of thehttpsAgent: new https.Agent
one? – Micael Levi Commented Jul 3, 2021 at 23:32 -
@MicaelLevi Yes, the error is on the
httpsAgent: new https.Agent
line. I import currently as follows:import https from 'https';
at the top of the file. – Andrew Commented Jul 3, 2021 at 23:42 -
1
@MicaelLevi you were correct, the issue was with how I was importing.
import https from 'https'
did not work butimport { Agent } from 'https'
did. Thank you! – Andrew Commented Jul 3, 2021 at 23:52
1 Answer
Reset to default 6instead of import https from 'https';
you should use the namespace import: import * as https from 'https';
or set the esModuleInterop
to true
in your tsconfig file (under pilerOptions
)