GlideHTTPRequest - グローバル
GlideHTTPRequest API は、Glide HTTP 要求で一般的な機能を実行するユーティリティメソッドを提供します。
この API は、グローバルサーバーサイドスクリプトで使用できます。このクラスを使用するには、コンストラクターを使用して GlideHTTPRequest オブジェクトをインスタンス化します。コンストラクターでは、入力パラメーターとしてエンドポイント URL が必要です。
GlideHTTPRequest - addHeader(文字列名, 文字列値)
HTTP 要求にヘッダーを追加します。
| 名前 | タイプ | 説明 |
|---|---|---|
| 名前 | 文字列 | ヘッダー名 ( Accept や Content-Type など)。 |
| 値 | 文字列 | ヘッダー値 ( application/json など)。 |
| タイプ | 説明 |
|---|---|
| なし |
この例では、要求ヘッダー「Accept」を追加し、ServiceNow インスタンスからインシデントの数を返し、JSON または XML 応答を解析します。
var instance = 'dev12345';
var username = 'admin';
var password = 'yourpassword';
// Instantiate request with ServiceNow API incidents table endpoint
var request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');
// Add authentication data
request.setBasicAuth(username, password);
// Add the Accept header to get JSON response
request.addHeader('Accept', 'application/json');
// Execute the GET request
var response = request.get();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
gs.print('(JSON) Incidents returned: ' + JSON.parse(response.getBody()).result.length);
// Replace the Accept header to get XML response
request.addHeader('Accept', 'application/xml');
// Execute the GET request
var response = request.get();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
gs.print('(XML) Incidents returned: ' + gs.xmlToJSON(response.getBody()).response.result.length);
出力
200
(JSON) Incidents returned: 66
200
(XML) Incidents returned: 66
GlideHTTPRequest - addParameter(文字列名, 文字列値)
HTTP 要求にパラメーターを追加します。
| 名前 | タイプ | 説明 |
|---|---|---|
| 名前 | 文字列 | 追加するパラメーター ( sysparm_limit など)。 |
| 値 | 文字列 | パラメーターの値。 |
| タイプ | 説明 |
|---|---|
| なし |
この例では、REST エンドポイント呼び出しに sysparm_limit パラメーターを追加して、返される応答の数を制限する方法を示します。
var instance = 'dev12345';
var username = 'admin';
var password = 'yourpassword';
// Instantiate request with ServiceNow API incidents table endpoint
var request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');
// Add authentication data
request.setBasicAuth(username, password);
// Add the 'sysparm_limit' parameter to limit the number of records returned
request.addParameter('sysparm_limit', 1);
// Execute the GET request
var response = request.get();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
gs.print('Incidents returned: ' + JSON.parse(response.getBody()).result.length);
出力:
200
Incidents returned: 1
GlideHTTPRequest - setBasicAuth(文字列 userName, 文字列 password)
ベーシック認証のユーザー名とパスワードを設定します。
| 名前 | タイプ | 説明 |
|---|---|---|
| userName | 文字列 | 認証に使用するユーザー名。 |
| パスワード | 文字列 | 認証に使用するユーザーのパスワード。 |
| タイプ | 説明 |
|---|---|
| なし |
この例では、 setBasicAuth() メソッドを使用して、関連する REST エンドポイント呼び出しのユーザー名とパスワードを設定する方法を示します。
var instance = 'dev12345';
var username = 'admin';
var password = 'yourpassword';
// Instantiate request with ServiceNow API incidents table endpoint
var request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');
// Add authentication data
request.setBasicAuth(username, password);
// Add the Accept header to get JSON response
request.addHeader('Accept', 'application/json');
// Execute the GET request
var response = request.get();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
gs.print('(JSON) Incidents returned: ' + JSON.parse(response.getBody()).result.length);
// Replace the Accept header to get XML response
request.addHeader('Accept', 'application/xml');
// Execute the GET request
var response = request.get();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
gs.print('(XML) Incidents returned: ' + gs.xmlToJSON(response.getBody()).response.result.length);
出力
200
(JSON) Incidents returned: 66
200
(XML) Incidents returned: 66
GlideHTTPRequest - setContentType(String type)
HTTP 要求の Content-Type ヘッダーを指定された値に設定します。
| 名前 | タイプ | 説明 |
|---|---|---|
| タイプ | 文字列 | 設定するコンテンツタイプ ( application/json や multipart/form-data など)。Content-Type の詳細については、「 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type」を参照してください。 |
| タイプ | 説明 |
|---|---|
| なし |
この例では、setContentType() メソッドを使用して REST エンドポイント呼び出しの Content-Type 要求ヘッダーを設定する方法を示します。
var instance = 'dev12345';
var username = 'admin';
var password = 'yourpassword';
// Instantiate request with ServiceNow API incidents table endpoint
var request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');
// Add authentication data
request.setBasicAuth(username, password);
// Set up incident record to post
// Set the Content-Type of the POST
request.setContentType('application/json');
// Execute the POST request
var response = request.post();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
出力
200
GlideHTTPRequest:setFollowRedirect(ブール値 followRedirect)
REST エンドポイント呼び出しのフォローリダイレクトオプションを有効または無効にします。
HTTP リダイレクトの詳細については、「 https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections」を参照してください。
| 名前 | タイプ | 説明 |
|---|---|---|
| フォローリダイレクト | ブール | エンドポイントがエンドポイントによって返された URL リダイレクトに従うかどうかを示すフラグ。 有効な値: デフォルト:true
|
| タイプ | 説明 |
|---|---|
| なし |
この例では、 setFollowRedirect() メソッドを使用してエンドポイント呼び出しのリダイレクトをオフにする方法を示します。
var instance = 'dev12345';
var username = 'admin';
var password = 'yourpassword';
// Instantiate request with ServiceNow API incidents table endpoint
var request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');
// Add authentication data
request.setBasicAuth(username, password);
// Add the Accept header to get JSON response
request.addHeader('Accept', 'application/json');
// Turn off follow redirect - default is on (true)
request.setFollowRedirect(false);
// Execute the GET request
var response = request.get();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
出力
200
GlideHTTPRequest - setHttpTimeout(int timeout)
HTTP タイムアウト値をミリ秒単位で設定します。
| 名前 | タイプ | 説明 |
|---|---|---|
| タイムアウト | 整数 | 設定するタイムアウト値。 単位:ミリ秒 |
| タイプ | 説明 |
|---|---|
| なし |
この例では、 setTimeout() メソッドを使用してエンドポイント呼び出しのタイムアウト値を設定する方法を示します。
var instance = 'dev12345';
var username = 'admin';
var password = 'yourpassword';
// Instantiate request with ServiceNow API incidents table endpoint
var request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');
// Add authentication data
request.setBasicAuth(username, password);
// Add the Accept header to get JSON response
request.addHeader('Accept', 'application/json');
// Set the time out value
request.setHttpTimeOut(1000);
// Execute the GET request
var response = request.get();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
出力
200
GlideHTTPRequest:setLogLevel(文字列 logLevel)
HTTP 要求のログレベルを設定します。
| 名前 | タイプ | 説明 |
|---|---|---|
| logLevel | 文字列 | 利用可能なログ記録のレベル。 注: パフォーマンス上の理由から、本番環境では HTTP 要求のログ記録を basic のままにしておくことをお勧めします。 有効な値:
デフォルト:基本 |
| タイプ | 説明 |
|---|---|
| なし |
この例では、 setLogLevel() メソッドを使用してエンドポイント呼び出しのログレベルを設定する方法を示します。
var instance = 'dev12345';
var username = 'admin';
var password = 'yourpassword';
// Instantiate request with ServiceNow API incidents table endpoint
var request = new GlideHTTPRequest('https://'+instance+'.service-now.com/api/now/table/incident');
// Add authentication data
request.setBasicAuth(username, password);
// Add the Accept header to get JSON response
request.addHeader('Accept', 'application/json');
// Set the time out value
request.setLogLevel(elevated);
// Execute the GET request
var response = request.get();
// Print the results: status code and number of records returned
gs.print(response.getStatusCode());
出力
200
GlideHTTPRequest - setupProxy(文字列 host, 文字列 port)
関連する REST 呼び出しのプロキシ ホストとポートを設定します。
| 名前 | タイプ | 説明 |
|---|---|---|
| host | 文字列 | プロキシホスト |
| ポート | 文字列 | プロキシのポート |
| タイプ | 説明 |
|---|---|
| なし |