NestJS Error https 통신중 unable to verify the first certificate 에러
NestJS으로 POST통신중 문제가 생겼고 해결을 해서 블로그에 남깁니다.
import axios from 'axios';
import { PushBodyDto } from 'src/type/sendPushMessage.dto';
require('dotenv').config();
const https = require('https');
export async function sendPushMessage(body: PushBodyDto) {
return await axios
.post(process.env.NESTJS_RESTAPI_URL + '/push-noti/send/message', {
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
data: body,
})
.then((res) => {
console.log('푸시메세지 보낸후 데이터 : ', res.data);
return res.data;
})
.catch((err) => {
return err;
})
.finally(() => console.log('푸시메세지 요청 완료!'));
}
코드1) 초기 Post Request 코드
위 코드에서 POST요청으로 다른서버에 Push요청을 보내는 코드 입니다. 오류 발생시 무시하는 코드(rejectUnauthorized : true)를 넣었음에도 불구하고 아래와 같이 응답이 왔습니다.
응답데이터(Click)
---------------------------------------------
{
"message": "unable to verify the first certificate",
"name": "Error",
"stack": "Error: unable to verify the first certificate\n at TLSSocket.onConnectSecure (node:_tls_wrap:1530:34)\n at TLSSocket.emit (node:events:394:28)\n at TLSSocket._finishInit (node:_tls_wrap:944:8)\n at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:725:12)",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"env": {},
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"User-Agent": "axios/0.27.2",
"Content-Length": 758
},
"method": "post",
"url": "https://pushData.co.kr:7495/push-noti/send/message",
"data": "{\"httpsAgent\":{\"_events\":{},\"_eventsCount\":2,\"defaultPort\":443,\"protocol\":\"https:\",\"options\":{\"rejectUnauthorized\":false,\"path\":null},\"requests\":{},\"sockets\":{},\"freeSockets\":{},\"keepAliveMsecs\":1000,\"keepAlive\":false,\"maxSockets\":null,\"maxFreeSockets\":256,\"scheduling\":\"lifo\",\"maxTotalSockets\":null,\"totalSocketCount\":0,\"maxCachedSessions\":100,\"_sessionCache\":{\"map\":{},\"list\":[]}},\"data\":{\"user_id\":[\"56be9770-f807-49ca-81c3-81a9806cc38a\"],\"title\":\"상금을 잡자(Test Message)\",\"body\":\"게임시간이 10분 남았습니다.(Test Message)\",\"reservation_at\":\"2022-08-12T05:56:19.433Z\",\"type\":\"openWebLink\",\"bottomIndex\":1,\"webLinkUrl\":\"https://namu.wiki/w/PORTAL%202\",\"routeName\":\"apple\",\"parameter\":\"{\\\"when\\\":\\\"now\\\",\\\"who\\\":\\\"all\\\"}\",\"sendNow\":false}}"
},
"code": "UNABLE_TO_VERIFY_LEAF_SIGNATURE",
"status": null
}
---------------------------------------------
import axios from 'axios';
import { PushBodyDto } from 'src/type/sendPushMessage.dto';
require('dotenv').config();
const https = require('https');
export async function sendPushMessage(body: PushBodyDto) {
return await axios({
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
method: 'post',
url: process.env.NESTJS_RESTAPI_URL + '/push-noti/send/push_message', // important change
data: body,
})
.then((res) => {
console.log('푸시메세지 보낸후 데이터 : ', res.data);
return res.data;
})
.catch((err) => {
return err;
})
.finally(() => console.log('푸시메세지 요청 완료!'));
}
코드2) 수정한 코드
코드2처럼 수정후 다시 Post요청을하니 제대로 응답이 왔습니다.
댓글
댓글 쓰기