URL Authentication - 401 error

my ping synthetic returning 401 and i need a API testing script to pass credentails to url to authenticate. can i get the sample script

Hi @muralikrishna.nutakk - You could look at my sample API script for your starting point.

Hello stefan,

The script mentioned in your post does some specific check’s based on maintenance window. But my requirement is a bit different and all i want is to verify the url availability with passing basic auth credentails and return if url is not avilable.

i used the following but it is still not giving desired results and returns with 401 error

var assert = require(‘assert’);

var serviceUrl = “https://discuss.newrelic.com”;

var options = {
url: serviceUrl + “/t/url-authentication-401-error/42270”,
body: ‘{“login”: “username”, “password” : “password”, “tokenExpirationInMinutes”: 1}’,
headers: {
‘Content-Type’: ‘application/json’

function callback(error, response, body) {
console.log('Response code: ’ + response.statusCode);
if (!error && response.statusCode == 200) {
console.log(‘POST authentication was successful’);
else {
console.log(‘POST failed’);

assert.ok(response.statusCode == 401, ‘Expected 401 OK response’);
assert.ok(!error, ‘We did not expect an error’);

$http.get(options, callback);

Hi @muralikrishna.nutakk

Is it a HTTP Authentication in the browser or is it a login page generate as part of your application?

The reason I ask, is if it’s a pure HTTP authentication, you can likely just pass the username and password in the URL request.


If it’s an onpage login generated by your application, you would need to interact with the elements and maybe use sendkeys to enter the username and password into the application and then click the login page.

Thanks. I Was able to execute my script by placing the credentails in Header instead of the response body

Awesome. Great job. Hope your script is good and working as expected now! :slight_smile: