ダイレクト RESTMessageV2 の例

  • リリースバージョン: Yokohama
  • 更新日 2025年01月30日
  • 所要時間:2分
  • 送信 REST メッセージをエンドポイントに直接送信できます。

    この例では、スクリプトは株価情報を要求する REST メッセージを送信し、応答を待機します。Web サービスプロバイダーからの応答がない場合や、指定された REST メッセージレコードが使用できない場合、スクリプトはエラーをスローし、この例では try-catch ブロックによって処理されます。

    var requestBody;
    var responseBody;
    var status;
    var sm;
    try{
    	sm = new sn_ws.RESTMessageV2("Yahoo Finance", "get");  // Might throw exception if message doesn't exist or not visible due to scope.
    	sm.setBasicAuth("admin","admin");
    	sm.setStringParameter("symbol", "NOW");
    	sm.setStringParameterNoEscape("xml_data","<data>test</data>");
    	sm.setHttpTimeout(10000); //In milliseconds. Wait at most 10 seconds for response from http request.
    
    	response = sm.execute();//Might throw exception if http connection timed out or some issue with sending request itself because of encryption/decryption of password.
    	responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();
    	status = response.getStatusCode();
    } catch(ex) {
    	responseBody = ex.getMessage();
    	status = '500';
    } finally {
    	requestBody = sm ? sm.getRequestBody():null;
    }
    gs.info("Request Body: " + requestBody);
    gs.info("Response: " + responseBody);
    gs.info("HTTP Status: " + status);