RESTサービス

最終公開日 : Sep 16, 2026

はじめに

EBX® スクリプトは、HTTP API を使用して外部RESTサービスを呼び出すことができます。このAPIは以下のメソッドを提供します。
  • HTTPリクエストの作成
  • ヘッダーへのパラメーターの追加
  • レスポンスの読み取り(メッセージボディ、HTTPコードなど)
    サーバーリソースに対して操作を実行するには、以下のHTTPメソッドを使用します。
  • GET : サーバーからリソースまたはコレクションを取得
  • POST : サーバー上に新しいリソースを作成するためのデータを送信
  • PUT : サーバー上の既存のリソースを更新するためのデータを送信
  • DELETE : サーバー上のリソースを削除
    RESTサービスは、EBX® スクリプトIDEを使用して、テーブルトリガーおよびスクリプトタスクから呼び出すことができます。

設定と認証

スクリプトで呼び出される各RESTサービスは、以下のテーブルで設定します。 管理 > 統合 > 外部サービス > 設定。以下のプロパティが必要です。
  • 設定ID : REST設定の一意の識別子。APIでHTTPリクエストを作成するために必要です。
  • ベースURL : 呼び出すサーバーのURL。クライアントとサーバー間の通信を暗号化するにはHTTPSを使用します。URLの形式は次のとおりです。
      http[s]://<host>[:<port>]/{path}{?<queryParameters>}

      where

      - host and port: defines the server address where the API resides.
      - path: is the path of the specific resource within the API.
      - queryParameters: are optional parameters used to filter or refine the resource.
  • 使用方法 : サービスの利用方法を定義します。AIアシスタントおよび/またはRESTサービス。
  • 認証タイプ : リソースへのアクセスに使用される認証タイプを指定します。以下のタイプがサポートされています。
タイプ 説明
Basic 有効なユーザー名とパスワードを必要とする、シンプルで高速なHTTP認証方法です。資格情報はBase64でエンコードされ、HTTPリクエストヘッダーに含まれます。認証コードをリクエストのヘッダーに追加する必要はありません。サーバーへの各アクセス前に自動的に含まれます。
EBX EBX組み込みRESTfulサービスへのアクセスを可能にします。認証トークンを取得するには、有効なユーザー名とパスワードを提供する必要があります。トークンにはタイムアウト期間があります。EBX® サーバーへの各アクセス時に、新しいトークンがリクエストされ、HTTPリクエストヘッダーに含まれます。スクリプトからEBXトークンをヘッダーに追加する必要はありません。EBX® サーバーへの各アクセス前に自動的に含まれます。
Custom 有効なキー名とキー値を提供することで、カスタム認証を可能にします。キーの値は秘密にすることができ、キーストアに基づいて暗号化されます。このカスタムタイプは、APIキー、トークン、OAuthなどの認証を使用してサービスを呼び出すために使用できます。
None 認証は不要です。

RESTサービスの呼び出し

EBX® スクリプトからRESTサービスを呼び出すには、次の手順を実行します。
  • 管理 > 統合 > 外部サービス > 設定 に移動し、サーバーを設定します。設定ID、URL、認証タイプ、および資格情報を設定します。
  • EBX® スクリプトIDE内でテーブルトリガーまたはスクリプトタスクにアクセスします。
  • 指定された設定IDを使用してRESTサービスを呼び出します。

リクエストの作成

core.http APIを使用して、新しいHTTPリクエストを作成できます。このAPIは、リクエストの設定、パス、パラメーター、およびヘッダーフィールドの追加のためのメソッドを提供します。
以下の例は、RESTサービスを呼び出すいくつかのスクリプトを示しています。
例 : 特定のカテゴリのすべての製品を取得するリクエストの作成
GET https://api.example.com/v1/products/?sort=desc&category=jewelery
以下の設定が使用されます。
  • 設定ID: ebx-1
  • URL: https://api.example.com
  • 使用方法: Rest
  • 認証タイプ: basic
  • 資格情報: ユーザー名、パスワード
    uses core.http as client;
    ...
    procedure getJeweleryProducts()
    begin

      // create a request with basic authentication
      var request := client.createRequest('ebx-1');

      // include content type in the header
      client.setHeader(request,'Content-Type','application/json');

      // add paths
      client.addPath(request,'v1');
      client.addPath(request,'products');

      // add query parameters
      client.addParameter(request,'category','jewelery');
      client.addParameter(request,'sort','desc');
      ...
      // sends a request to get products of a specific category
      ...
    end
    例 : 結果を制限してすべての製品を取得するリクエストの作成
    GET https://api.example.com/v1/products?limit=15
    以下の設定が使用されます。
  • 設定ID: ebx-2
  • URL: https://api.example.com
  • 使用方法: Rest
  • 認証タイプ: custom
  • キー名: api\_key
  • キー値: access\_key
    uses core.http as client;
    ...
    procedure getProducts()
    begin
      // create a request with custom authentication
      var request := client.createRequest('ebx-2');
      client.addPath(request,'v1');
      client.addPath(request,'products');
      client.addParameter(request,'limit','15');

      // get api_key value saved during the configuration of the REST service
      var accessKey := client.getConfigValue(request,'api_key');
      if not isNull(accessKey) then
      begin
        // include the authorization api_key in the header
        client.setHeader(request,'Authorization','api_key '| accessKey);
        ...
        // sends a request to get all products
        ...
      end
    end
    例 : EBX組み込みRESTfulサービスを呼び出してユーザーメールを更新するリクエストの作成
    PUT https://ebx-example.io/test/ebx-dataservices/rest/data/v1/Bdataspace/dataset/root/user/ID6766
    以下の設定が使用されます。
  • 設定ID: ebx-3
  • URL: https://ebx-example.io/test/ebx-dataservices
  • 使用方法: Rest
  • 認証タイプ: ebx
  • 資格情報: ユーザー名、パスワード
    uses core.http as client;
    ...
    procedure updateUserEmail()
    begin

    var request:= client.createRequest('ebx-3');

    // add paths
    client.addPath(request,'data/v1/Bdataspace/dataset/root/user/ID6766');

    // include the content type in the header
    client.setHeader(request,'Content-Type','application/json');

    // sends a put request to update the user email
    ...
    end
    例 : 都市に関する情報を取得するリクエストの作成
    GET https://geo.api.gouv.fr/communes?codePostal=92160&fields=nom,departement,region
    以下の設定が使用されます。
  • 設定ID: ebx-4
  • URL: https://geo.api.gouv.fr/communes
  • 使用方法: Rest
  • 認証タイプ: none
    uses core.http as client;
    ...
    procedure getCityInfo()
    begin

       var request:= client.createRequest('ebx-4);

       // add query parameters
       client.addParameter(request,'codePostal','92160');
       client.addParameter(request,'fields','nom,departement,region');

       // get the url of the request.
       var message:='Request URL : '|client.getRequestUrl(request);

       // sends a request to get information about a city specified by its zipcode 92160
       ...
    end

リクエストの送信

core.http APIは、RESTサービスを呼び出すためのいくつかのHTTPメソッド(GET、PUT、POST、DELETE)を提供します。以下の例は、HTTPメソッドを使用して製品に対する操作を実行するいくつかのスクリプトを示しています。

GET

例 : サーバーから単一の製品を取得
GET https://api.example.com/v1/products/P87RZ63
以下のスクリプトは、識別子によって単一の製品を取得するためのGETリクエストを送信します。
uses core.http as client;

procedure getSingleProduct()
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'v1/products/P87RZ63');

  // get response
  var response := client.get(request);
  ...
end

POST

例 : サーバー上に新しい製品を作成
POST https://api.example.com/v1/products
以下のスクリプトは、新しい製品を追加するためのPOSTリクエストを送信します。
uses core.http as client;

procedure addProduct()
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'v1/products');
  client.setHeader(request,'Content-Type','application/json');
  var body:='{
                 "id": "L32LQ",
                 "title": "TV LED Sony",
                 "price": 1543,
                 "image": "https://tv.pravatar.sony",
                 "category": "electronic"
             }';

 // sends a POST request
  var response := client.post(request,body);
  ...
end

PUT

例 : サーバー上の既存の製品を更新
PUT https://api.example.com/v1/products/L32LQ
以下のスクリプトは、特定の製品を更新するためのPUTリクエストを送信します。
uses core.http as client;
...
procedure updateProduct()
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'v1/products/L32LQ');

  var body:='{
                 "price": 789
             }';

 // sends a PUT request
  var response := client.put(request,body);
  ...
end

DELETE

例 : サーバー上の既存の製品を削除
DELETE https://api.example.com/v1/products/L32LQ
以下のスクリプトは、特定の製品を削除するためのDELETEリクエストを送信します。
uses core.http as client;
...
procedure deleteProduct()
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'v1/products/L32LQ');
  var response := client.delete(request);
  ...
end

レスポンスの読み取り

core.http APIを使用して、レスポンスのステータスコード、ヘッダー、およびボディを読み取ります。json.httpを使用して、レスポンスボディを解析し、JSONデータを管理します。
例 : 特定の製品に関する情報を取得するリクエストのボディレスポンスの読み取り
uses core.http as client;
uses core.json as json;
uses core.log as log;
...
procedure getProductInfo()
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'v1/products/P87RZ63');

  // send a get request
  var response := client.get(request);

  // get status code of the response
  var status:=client.getStatusCode(response);
  if (status = 200) then
  begin

     if (client.hasBody(response)) then
     begin
       // get the first content encoding from the response header
       var encoding := client.getHeaderFirstValue(response,'content-encoding');

       // get the list of access allowed methods from the response header
       var allowedMethod := client.getHeaderValues(response,'access-control-allow-methods');

       // get the body of the response as object
       var body:=client.getJsonBody(response).asObject;

       // retrieve information about the product
       log.info('id :' | json.getString(body ,'id'));
       log.info('title :' | json.getString(body ,'title'));
       log.info('price :' | json.getString(body ,'price'));
     end
    ...
  end
end

レスポンスエラーの処理

エラーが発生した場合は、ステータスコードとレスポンスボディで報告される追加情報を確認してください。core.http APIは、リクエストURLの取得、ステータスコードの取得、およびレスポンスボディ内のエラーメッセージの読み取りのためのメソッドを提供します。
例 : EBX組み込みRESTfulサービスにユーザーメールを更新するリクエストを送信する際、レスポンスエラーをログに記録
uses core.http as client;
uses core.json as json;
uses core.log as log;
...
procedure updateEmail()
begin

   var request:= client.createRequest('ebx-3');
   client.addPath(request,'data/v1/Bdataspace/dataset/root/user/ID66Z7');

   // set a new timeout for the request
   client.setTimeout(request,800);

   var json:='{
                   "content": {

                     "email": {
                       "content": "loana-provost@gmail.com"
                     }
                   }
              }';

    // update user email
    var response := client.put(request,json);
    var status:=client.getStatusCode(response);

   // read the response
   if (status>=200 and status <=300) then
    begin
       log.info('The request was successful '|status);
      if (client.hasBody(response)) then
      begin
        log.info('Response body: ' | client.getStringBody(response));
      end
    end
   else
      begin
        log.info('Error when sending the request');
        // log the url of the request.
        log.info('Request URL : '|client.getRequestUrl(request));

        // log the url of the response.
        log.info('Response URL : '|client.getResponseUrl(response));

        var body:=client.getJsonBody(response).asObject;
        // log error status code
        log.info('Code : ' | json.getDecimal(body,'code'));

        // log error message
        var errors:=json.getArray(body,'errors');
        for error in errors do
         begin
         log.info('Message : ' |  json.getString(error.asObject,'message'));
         end
      end
end