1

I am trying to post at the end point /api/mid/sa/metrics with the below code, but getting the error of Cannot read property 'statuscode' of undefined :

Node js version used is 8.11.0. Please help if I am missing anything.

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)

code used for this is a java script which has the below code for posting the raw data at the endpoint.

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)
})
})
Savi
  • 11
  • 1
  • What does `console.log('Error:', error);` output within the `request.post()` callback? – Jonathan Lonowski May 13 '18 at 16:43
  • 2
    it's `response.statusCode` & `response` is undefined because there is an error. Check against error first. – Marcos Casagrande May 13 '18 at 16:47
  • After adding console.log('Error:', error); in the request.post(), getting the below error C:\Program Files\nodejs\node_modules\npm>node MetricOne10.js Error: { Error: connect ECONNREFUSED 192.168.3.57:443 at Object._errnoException (util.js:1022:11) at _exceptionWithHostPort (util.js:1044:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14) code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', address: '192.168.3.57', port: 443 } – Savi May 14 '18 at 05:57
  • How can I give proxy in the above code. The server is connected via the proxy? – Savi May 14 '18 at 06:18

0 Answers0