Synthetics API Test with bad request

Hi there,

I’m fairly new to New Relic but I’m trying to find a way to pass the validation test.
I tried to send POST request to get the user access token, and then use that token to send a GET request to fetch some user information.

However, I always got the error: TypeError: Cannot read property ‘statusCode’ of undefined. I am thinking it might be some syntax error or some bad request from my side. Can you take a look of my code? Let me know if you need more information. Thanks in advance.

var info,token;
var assert = require(‘assert’);
var $http = require(‘request’)

var options = {
uri: ‘https://qa-apixxxxxx/token’,

body: {
‘username’:‘xxxxxxx’,
‘password’:‘xxxxxxxx’,
‘grant_type’:‘password’,
‘scope’:‘openid’
},

headers: {
‘Content-Type’: ‘application/x-www-form-urlencoded’,
‘Accept’:‘text/plain’
}
}

$http.post(options,function (err, response, body) {
console.log(response.statusCode);
console.log(body);
info = JSON.parse(body);
token=info.access_token;
console.log(‘Security token retrieved successfully’);
console.log(token);
assert.ok(response.statusCode == 200, ‘Expected 200 response’);
}
);

var options2 = {
uri: ‘https://qa-apixxxxx/users’,

headers: {
// ‘access_key’: token,
‘Accept’:‘application/json’,
‘Authorization’: token
//‘Content-Type’: ‘application/json’
},
body: {
‘username’:‘xxxxxxxx’
}
}

$http.get(options2,function (err, response, body) {
console.log(response.statusCode);
console.log(body);
assert.ok(response.statusCode == 200, ‘Expected 200 response’);
}
);

Hi there @Wei.Wang-non-empl -

Is it possible that you are having trouble with a timeout? That could cause this error. Found this reference to the same issue:

Continuing the discussion from Synthetics API Test with bad request:

Hi,

Did you get this fixed as I am also having the same issue?

Cheers,
Amit

Hi Amit,

Yes, it’s been fixed.

Cheers,
Wei

@Wei.Wang-non-empl

Can you share what helped to fix your script? It sounds like @amit.tarneja1 is facing the same issue and would appreciate some info on how you fixed your script!

Hi, it was the format of the code that caused the problem!

Thanks @Wei.Wang-non-empl :slight_smile: