我正在使用https库获取https请求的响应,问题是,当https.get
处理Url时,它通过回调函数在响应中提供了正确的响应头和状态代码,但响应主体为undefined
(当我通过邮递员之类的工具使用此URL时会收到此消息。)
这是我的回调函数:
onRequestComplete( received ) {
try {
logger.log( 'Message received from endpoint' );
logger.log( 'statusCode: ' + received.statusCode );
logger.log( 'headers: ' + JSON.stringify( received.headers ) );
let data = "";
logger.log('will now log data: ');
received.on('data', function (d) {
logger.log('logging data: ');
logger.log(d);
data += d;
});
let this_ = this;
received.on( 'end', function() {
let response: string = data.toString();
let endpoint = this_.getEndPoint(this_.requestMessage.DESTINATION_NETWORK_ID);
this_.requestMessage.SOURCE_CHANNEL_ID = String(endpoint.CHANNEL_ID);
this_.requestMessage.SOURCE_NETWORK_ID = endpoint.NETWORK_ID;
this_.callbackFunction( this_.reference, received.headers, received.statusCode, response, this_.requestMessage )
} );
}
catch ( err ) {
logger.logError( err.message );
}
}
通过以下方式消费:
if ( this.requestMessage.httpmethod === "GET" || this.requestMessage.httpmethod === "DELETE" ) {
request = https.get( options, this.onRequestComplete.bind( this ) ).on('error', (e) => {
logger.logError('error occurred')
logger.logError(e);
}); }
登录回调函数时,我看到没有调用on
数据事件(received.on( 'data', function(d) {..})
。可能是什么原因?该代码可用于其他调用,唯一的区别是所处理的url的传输编码已分块。