ReferenceError: $http is not defined

I am try to write a simple script in new relic synthetics and am getting : ReferenceError: $http is not defined

Part of my code:

var assert = require('assert');
WEBSITE_URL = 'https://google.com/';

console.log(WEBSITE_URL);
$http.get(WEBSITE_URL, function(error, response, body){
    if (error)
    {
      console.log(error); // fail the test here if required
    }

Hi @jahved_cassim - have a look at my API Synthetic maintenance window workaround for sample code which may help.

2 Likes

Thanks Stefan, that’s really fantastic!!

@jahved_cassim - Are you still seeing the $http error about it not being defined? If so, I suspect it’s most likely because you are attempting to use the $http object in a Scripted Browser monitor type instead of an API monitor type.

1 Like

Hi,

Yes, you are absolutely correct. Actually, I need to define the variable

var $http = require(‘request’);

I have a few questions :

Question 1

After 180s – 3 min we have ‘Job Time out’ .Can we increase this time out session?

Question 2
Scripted Browser monitor type and API monitor type, I am getting NetworkError: Monitor produced no traffic.

But the log is displayed correctly. That’s why I have used sripted Browser monitor and passing the URL in the $browser.get(URL) to overcome the issue of “Monitor produced no traffic.”

Is there another way to do that??

Question 3: most Important part

Am using the $http.get but i found that for one of the URL the page is displayed but still response.status return 404: Not found

Can you tell me why we have this 404??

Answer 1. You can increase the timeout for browser synthetics. Search the documentation.

Answer 2. Happens if you do not browse to a page IIRC. Hence my sample includes a browse to url.

Answer 3. Ummm not sure.

Sorry for the lack of links but I’m on my mobile

@jahved_cassim To add on to @stefan_garnham responses

  1. You can adjust the browser timeout and some other timeouts via $browser.manage().timeouts() https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/scripting-monitors/synthetics-scripted-browser-reference#options but you cannot adjust the 3 minute overall script execution timeout. This is by design of Synthetics and not possible to change. Can I ask for some information about what you are trying to accomplish that takes more than 3 minutes?
  1. So it sounds like you are using scripted browser but manually defining $http. If you do that, the instance of the request module you have required in will not be the same as using an API test and using the pre-built $http module. For Synthetics to record network traffic you MUST use the $browser object for scripted browser tests and the $http object for API test types. You CAN manually require in additional modules but if those objects are not used by the test to do some network traffic and a Synthetics test that records no traffic will by definition fail.
  2. See my answer in question 2. I suspect your $http object is returning the 404 but you may have a manual $browser that is correctly getting the page but would have to look at your script to confirm.
1 Like

@sdelight && @stefan_garnham Copy to API Scripting and validate the code below – This is just a simple test:

var options = {
    //Define endpoint URI
    uri: ''
};
//Define expected results using callback function.
function callback (err, response){
//Log JSON results from endpoint to Synthetics console.
console.log(response.statusCode);

 console.log('done with script');
}
//Make GET request, passing in options and callback.
$http.get(options,callback);

Why do I get 404 which means not found but when i copy and paste the link in my firefox browser the page is open without any issue…

Even firebug give a response of 200 – I did check on https://httpstatus.io/ response is 200

Looks like you missed the method type: post or get.

@jahved_cassim - I added a bit of logging to your script to returning the headers and the response payload and it looks like your server is returning the 404 and a stack trace from your php app.

What I did is add the body parameter to the callback method and then log it via console.log as well as logging response.headers

I hope this helps you track down why the Synthetics API test is returning a 404.