通过oracle查询调用restful API

问题描述 投票:0回答:2

oracle中有没有办法通过oracle查询调用restful API? 案例:在数据库中插入某条记录后,触发器将使用 Json 格式调用机器外部的 API。

oracle-database rest api
2个回答
6
投票

INSERT
包装在存储过程中,并添加对
UTL_HTTP
包中的方法之一的调用,以调用外部 API。

类似(未经测试):

CREATE PROCEDURE YOUR_SCHEMA.CREATE_ITEM(
  i_value1 YOUR_SCHEMA.YOUR_TABLE.VALUE1%TYPE,
  i_value2 YOUR_SCHEMA.YOUR_TABLE.VALUE2%TYPE,
  i_value3 YOUR_SCHEMA.YOUR_TABLE.VALUE3%TYPE
)
IS
  req     UTL_HTTP.REQ;
  resp    UTL_HTTP.RESP;
  content VARCHAR2(4000);
  buffer  VARCHAR2(4000);
BEGIN
  INSERT INTO YOUR_SCHEMA.YOUR_TABLE (
    value1,
    value2,
    value3
  ) VALUES (
    i_value1,
    i_value2,
    i_value3
  );

  req := UTL_HTTP.BEGIN_REQUEST (
    url    => 'https://your_server.here/rest/items',
    method => 'POST'
  );

  content := '{'
    || '"value1":"' || value1 || '",' -- remember to escape special characters
    || '"value2":"' || value2 || '",'
    || '"value3":"' || value3 || '"'
    || '}';

  UTL_HTTP.SET_HEADER( req, 'content-type', 'application/json' ); 
  UTL_HTTP.SET_HEADER( req, 'Content-Length', length(content) );
  UTL_HTTP.WRITE_TEXT( r => req, data => content );
  resp := UTL_HTTP.GET_RESPONSE(req);

  BEGIN
    LOOP
      UTL_HTTP.READ_LINE( resp, buffer, TRUE );
      DBMS_OUTPUT.PUT_LINE( buffer );
    END LOOP;
    UTL_HTTP.END_RESPONSE(resp);
  EXCEPTION
    WHEN UTL_HTTP.END_OF_BODY THEN
      UTL_HTTP.END_RESPONSE(resp);
  END;
END;
/

0
投票

我认为您可以使用 Oracle 的内置包从 Oracle 查询中调用 RESTful API

UTL_HTTP
1 必要的特权

GRANT EXECUTE ON UTL_HTTP TO your_users;
GRANT EXECUTE ON DBMS_NETWORK_ACL_ADMIN TO your_users;

配置网络ACL允许Oracle用户访问特定的Web服务

BEGIN
  DBMS_NETWORK_ACL_ADMIN.create_acl(
    acl         => 'api_access.xml',
    description => 'Access to RESTful APIs',
    principal   => 'your_user',
    is_grant    => TRUE,
    privilege   => 'connect'
  );

  DBMS_NETWORK_ACL_ADMIN.assign_acl(
    acl  => 'api_access.xml',
    host => 'api.example.com',
    lower_port => 80,
    upper_port => 80
  );
END;
/

3 程序

CREATE OR REPLACE PROCEDURE call_rest_api (
  p_url IN VARCHAR2
) AS
  l_req    UTL_HTTP.req;
  l_resp   UTL_HTTP.resp;
  l_buffer VARCHAR2(32767);
BEGIN
  -- Set up HTTP request
  l_req := UTL_HTTP.begin_request(p_url, 'POST', 'HTTP/1.1');
  UTL_HTTP.set_header(l_req, 'Content-Type', 'application/json');
  
  -- Include any data you want to send (example JSON data)
  UTL_HTTP.write_text(l_req, '{"key":"value"}');

  -- Make the HTTP call
  l_resp := UTL_HTTP.get_response(l_req);

  -- Read the response
  BEGIN
    LOOP
      UTL_HTTP.read_text(l_resp, l_buffer);
      DBMS_OUTPUT.put_line(l_buffer);
    END LOOP;
  EXCEPTION
    WHEN UTL_HTTP.end_of_body THEN
      UTL_HTTP.end_response(l_resp);
  END;
  
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.put_line('Error: ' || SQLERRM);
    UTL_HTTP.end_response(l_resp);
END;
/

最后=触发

  REATE OR REPLACE TRIGGER after_insert_trigger
    AFTER INSERT ON your_table
    FOR EACH ROW
    BEGIN
      call_rest_api('http://api.your.com/resource');
    END;

/
© www.soinside.com 2019 - 2024. All rights reserved.