通过oracle查询调用restful API

时间:2018-04-11 07:43:05

标签: oracle rest api

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

1 个答案:

答案 0 :(得分:0)

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;
/