Rest API节点js

时间:2018-05-13 16:38:50

标签: node.js api

我正在尝试使用以下代码在结束点/ api / mid / sa / metrics发布,但是得到错误的无法读取未定义的属性'statuscode' :

使用的节点js版本是8.11.0。如果我遗漏任何东西,请帮忙。

TypeError: Cannot read property 'statuscode' of undefined
    at Request._callback (C:\Program Files\nodejs\node_modules\npm\MetricOne10.j
s:50:23)
    at self.callback (C:\Program Files\nodejs\node_modules\npm\node_modules\requ
est\request.js:186:22)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at Request.onRequestError (C:\Program Files\nodejs\node_modules\npm\node_mod
ules\request\request.js:878:8)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:387:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)

用于此目的的代码是一个java脚本,它具有以下用于在端点上发布原始数据的代码。

var request = require('request')
var fs = require('fs')
var now = +new Date()

function readContent(callback) {

    fs.readFile("C:\powershell\Config\OIconfig.txt","utf8", function(error, config) {
  if (error) return callback(error)
      callback(null,config)
    })

}

readContent(function (err, config) {


var userConfig = "admin";
var passConfig = "event@1ert";
var urlConfig = "http://**.**.**.**/api/mid/sa/metrics";


console.log(passConfig)

var options = {
    url: urlConfig,
    auth: {
        username: userConfig,
        password: passConfig

    },
    method: 'POST',
    json: [{
    'metric_type': 'CPU_Util',
    'resource': 'C:\\',
    'node': 'win-ces882ierw',
    'value': 10,
    'timestamp': now,
    'ci2metric_id': {
        'node': 'win-ces882ierw'
    },
    'source': 'MetricOne'
    }]
}


request.post(options, function (error, response, body){

    console.log(response)
    console.log(response.statuscode)
    console.log(response.statusMessage)
    console.log(now)
    console.log(userConfig)
    console.log(passConfig)
    console.log(urlConfig)
})
})

0 个答案:

没有答案