代码片段:REST客户端JS
function httpsRequest (method, data, options, callback) {
console.log('Request path=', options.host + ':' + options.port + options.path)
options.method = method
options.rejectUnauthorized = false
options.agent = false
var req = https.request(options, function (res) {
console.log('Response statuscode=', res.statusCode, 'Request=', req.method, req._headers.host, req.path)
var body = ''
res.on('data', function (d) {
body += d
})
res.on('end', function () {
var result = ('' + res.statusCode).match(/^[1-3]/)
if (result) {
if (body === '') {
console.log('HTTP WARN: BODAY=NULL ', 'Response statuscode=', res.statusCode, 'Request=', req.method, req._headers.host, req.path)
}
callback(res, body)
} else {
console.log('HTTP ERROR ', 'Response statuscode=', res.statusCode, 'Request=', req.method, req._headers.host, req.path)
callback('{}', '{}')
}
})
})
req.on('error', function (e) {
console.error('Return error when request ' + options.host, options.port, options.path, '--->>>', e)
callback('{}', '{}')
})
req.setTimeout(600000, function () {
console.error('TIMEOUT when request ' + options.host, options.port, options.path)
req.abort()
})
if (data) {
req.write(data, 'utf8')
}
req.end()
}
util.httpsPost = function (data, options, callback) {
httpsRequest('POST', data, options, callback)
}
util.httpsPut = function (data, options, callback) {
httpsRequest('PUT', data, options, callback)
}
util.httpsGet = function (options, callback) {
httpsRequest('GET', '', options, callback)
}
关键字:rest, JavaScript, http, options
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!