GlideHTTPRequest :グローバル

  • リリースバージョン: Zurich
  • 更新日 2025年07月31日
  • 所要時間:13分
  • GlideHTTPRequest API は、Glide HTTP 要求で一般的な機能を実行するためのユーティリティメソッドを提供します。

    この API は、グローバルなサーバー側スクリプトで使用できます。このクラスを使用するには、コンストラクターを使用して GlideHTTPRequest オブジェクトをインスタンス化します。コンストラクターには、入力パラメーターとしてエンドポイント URL が必要です。

    GlideHTTPRequest - addHeader(文字列 name, 文字列 value)

    HTTP 要求にヘッダーを追加します。

    表 : 1. パラメーター
    名前 タイプ 説明
    name 文字列 AcceptContent-Type などのヘッダー名。
    value 文字列 application/json などのヘッダー値。
    表 : 2. 返される内容
    タイプ 説明
    なし

    この例では、要求ヘッダー「Accept」を追加し、JSON または XML 応答を解析して、ServiceNow インスタンスからインシデントの数を返します。

    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(文字列 name, 文字列 value)

    HTTP 要求にパラメーターを追加します。

    表 : 3. パラメーター
    名前 タイプ 説明
    name 文字列 sysparm_limit などの追加するパラメーター。
    value 文字列 パラメーターの値。
    表 : 4. 返される内容
    タイプ 説明
    なし

    この例では、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)

    基本認証のユーザー名とパスワードを設定します。

    表 : 5. パラメーター
    名前 タイプ 説明
    userName 文字列 認証に使用するユーザー名。
    password 文字列 認証に使用するユーザーのパスワード。
    表 : 6. 返される内容
    タイプ 説明
    なし

    この例では、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(文字列 type)

    HTTP 要求のコンテンツタイプヘッダーを指定された値に設定します。

    表 : 7. パラメーター
    名前 タイプ 説明
    type 文字列 application/jsonmultipart/form-data など、設定するコンテンツタイプ。Content-Type の詳細については、「https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type」を参照してください。
    表 : 8. 返される内容
    タイプ 説明
    なし

    この例は、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 エンドポイント呼び出しの followRedirect オプションを有効または無効にします。

    HTTP リダイレクトの詳細については、「https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections」を参照してください。

    表 : 9. パラメーター
    名前 タイプ 説明
    followRedirect ブーリアン エンドポイントによって返された URL リダイレクトにエンドポイントが従う必要があるかどうかを示すフラグ。
    有効な値:
    • true:返されたリダイレクトに従います。
    • false:返されたリダイレクトを無視します。
    デフォルト:true
    表 : 10. 返される内容
    タイプ 説明
    なし

    この例は、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 タイムアウト値をミリ秒単位で設定します。

    表 : 11. パラメーター
    名前 タイプ 説明
    timeout 整数 設定するタイムアウト値。

    単位:ミリ秒

    表 : 12. 返される内容
    タイプ 説明
    なし

    この例では、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 要求のログレベルを設定します。

    表 : 13. パラメーター
    名前 タイプ 説明
    logLevel 文字列 利用可能なログ記録のレベル。
    注:
    パフォーマンス上の理由から、本番環境では HTTP 要求のログ記録を basic のままにすることをお勧めします。

    有効な値:

    • basic:ホスト、パス、応答ステータスなど、HTTP トランザクションの多くの属性に対応します。
    • elevated:basic のすべて、すべての要求ヘッダー、クエリ文字列、およびすべての応答ヘッダーが含まれます。
    • all:elevated のすべて、および要求本文と応答本文が含まれます。

    デフォルト:basic

    表 : 14. 返される内容
    タイプ 説明
    なし

    この例では、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 呼び出しのプロキシホストとポートを設定します。

    表 : 15. パラメーター
    名前 タイプ 説明
    host 文字列 プロキシホスト
    port 文字列 プロキシのポート
    表 : 16. 返される内容
    タイプ 説明
    なし