In the event of any inconsistency between the English version and the translated version, the English versionwill take priority. Please visit this page for more information.
API 테스트는 $http 객체를 통해 사용할 수 있는 got module에 의해 구동됩니다. $http 개체는 기본 사용 사례에 대해 모니터에 이전 버전과의 호환성을 제공하여 got와 함께 사용자 정의 request와 같은 경험을 제공합니다. Node.js 16 및 최신 스크립트 API 런타임에서 직접 request 사용하려고 시도하는 모든 고객에 대해 $http 객체가 제공하는 request와 유사한 환경도 반환됩니다.
$http 객체를 사용하는 한 결과 타이밍 세부정보가 제공됩니다. $http 객체가 다루지 않는 스크립팅된 API 사용 사례의 경우 $har 객체를 사용하여 맞춤 타이밍 세부정보를 보고 할 수 있습니다.
//Define your authentication credentials.
var myAccountID = '{YOUR_ACCOUNT_ID}';
var myAPIKey = '{YOUR_API_KEY}';
//Import the 'assert' module to validate results.
var assert = require('assert');
var options = {
//Define endpoint URL.
url: "https://insights-collector.newrelic.com/v1/accounts/"+myAccountID+"/events",
//Define body of POST request.
body: '[{"eventType":"SyntheticsEvent","integer1":1000,"integer2":2000}]',
//Define New Relic API key and expected data type.
headers: {
'Api-Key': myAPIKey,
'Content-Type': 'application/json'
}
};
//Define expected results using callback function.
function callback(error, response, body) {
//Log status code to Synthetics console.
console.log(response.statusCode + " status code")
//Verify endpoint returns 200 (OK) response code.
assert.ok(response.statusCode == 200, 'Expected 200 OK response');
//Verify that `body` contains element named `success` with a value of `true`.
assert.ok(body.success == true, 'Expected True results in Response Body, result was ' + body.success);
//Log end of script.
console.log("End reached");
}
//Make POST request, passing in options and callback.
$http.post(options, callback);
결과 검증
결과를 확인하려면 assert 모듈을 가져와 테스트 사례를 정의하세요. assert 메서드를 호출하여 엔드포인트의 응답을 확인합니다. assert 기능이 실패하면 전체 모니터가 실패한 검사로 간주됩니다. 이는 경고 알림 을 트리거하고 측정항목에 영향을 줄 수 있습니다.
중요
종합 모니터링은 예외가 발생하는 것을 허용하지 않습니다. 예외가 발생하면 스크립트가 실패합니다. assert 모듈을 사용하여 결과를 확인하고 console.log() 을 사용 하여 합성 콘솔에 결과를 기록합니다 .
이 예시는 Event API에 POST한 다음 응답이 {"success":true} 인지 확인합니다.