GlideHTTPRequest - グローバル

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

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

    GlideHTTPRequest - addHeader(文字列名, 文字列値)

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

    表 : 1. パラメーター
    名前 タイプ 説明
    名前 文字列 ヘッダー名 ( AcceptContent-Type など)。
    文字列 ヘッダー値 ( application/json など)。
    表 : 2. 戻り値
    タイプ 説明
    なし

    この例では、要求ヘッダー「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 要求にパラメーターを追加します。

    表 : 3. パラメーター
    名前 タイプ 説明
    名前 文字列 追加するパラメーター ( sysparm_limit など)。
    文字列 パラメーターの値。
    表 : 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 文字列 認証に使用するユーザー名。
    パスワード 文字列 認証に使用するユーザーのパスワード。
    表 : 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(String type)

    HTTP 要求の Content-Type ヘッダーを指定された値に設定します。

    表 : 7. パラメーター
    名前 タイプ 説明
    タイプ 文字列 設定するコンテンツタイプ ( 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 エンドポイント呼び出しのフォローリダイレクトオプションを有効または無効にします。

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

    表 : 9. パラメーター
    名前 タイプ 説明
    フォローリダイレクト ブール エンドポイントがエンドポイントによって返された 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. パラメーター
    名前 タイプ 説明
    タイムアウト 整数 設定するタイムアウト値。

    単位:ミリ秒

    表 : 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 トランザクションの多くの属性をカバーします。
    • 昇格:すべての basic、すべての要求ヘッダー、クエリ文字列、およびすべての応答ヘッダーが含まれます。
    • all: elevatedすべてと、要求本文と応答本文が含まれます。

    デフォルト:基本

    表 : 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 文字列 プロキシホスト
    ポート 文字列 プロキシのポート
    表 : 16. 戻り値
    タイプ 説明
    なし