oracle 如何将CURL命令转换为PL/SQL?

sycxhyv7  于 2023-04-20  发布在  Oracle
关注(0)|答案(1)|浏览(187)

我有一个curl命令,我需要在使用PL/SQL的程序中使用这个命令。
这是命令:

curl -X 'POST' \
  'https://gw-fatoora.zatca.gov.sa/e-invoicing/developer-portal/compliance' \
  -H 'accept: application/json' \
  -H 'OTP: 123345' \
  -H 'Accept-Version: V2' \
  -H 'Content-Type: application/json' \
  -d '{
  "csr": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tL1WVh3eUxUSXpOSHd6TFRNMU5ERWYKTUIwR0NnbVNKb21UOGl4a0FRRU1Eek14TURFM05UTTVOelF3TURBd016RU5NQXNHQTFVRURBd0VNVEV3TURFUQpNQTRHQTFVRUdnd0hXbUYwWTJFZ016RVlNQllHQTFVRUR3d1BSbTl2WkNCQ2RYTnphVzVsYzNNek1Bb0dDQ3FHClNNNDlCQU1DQTBjQU1FUUNJQ3JyTzdtSzZWZTZNTmIrSlNJRkRmK0FGMjhqV2ZJYTNIdzlhWEdVOS9KbkFpQXIKSnBVc0h4Z1RrOGtQZTRQSnNJVGJJYXlTeUh2emZwdHFFTWZEajdQN2F3PT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUgUkVRVUVTVC0tLS0t"
}'

我试过的:
我搜索了一个CURL转换器,我发现了这个伟大的网站:
https://curlconverter.com
使用它将CURL转换为Python命令非常有用,但我没有找到pl SQL
谁能帮帮我。
谢谢

eqoofvh9

eqoofvh91#

希望这对你有帮助…
创建一个函数,如下所示,并以endpoint和json字符串为参数调用该函数。

CREATE OR REPLACE FUNCTION SEND_DB_REQUST (
                                        P_API     VARCHAR2,
                                        P_CONTENT VARCHAR2
                                      ) RETURN VARCHAR2
IS
REQ UTL_HTTP.REQ;
RES UTL_HTTP.RESP;
BUFFER VARCHAR2(4000); 
RESPONS VARCHAR2(4000);
BEGIN
 -- Defined Http Version And Request Type
 REQ := UTL_HTTP.BEGIN_REQUEST(P_API, 'POST',' HTTP/1.1');

 -- Defined Browser Type
 UTL_HTTP.SET_HEADER(REQ, 'user-agent', 'mozilla/4.0'); 
 
 -- Defined Content Type (JSON - XML .. etc)
 UTL_HTTP.SET_HEADER(REQ, 'content-type', 'application/json'); 
 UTL_HTTP.SET_HEADER(REQ, 'accept', 'application/json'); 
 UTL_HTTP.SET_HEADER(REQ, 'OTP', '123345'); 
 UTL_HTTP.SET_HEADER(REQ, 'Accept-Version', 'V2'); 

 -- Defined Content Length
 UTL_HTTP.SET_HEADER(REQ, 'Content-Length', LENGTH(P_CONTENT));

 -- Sent HTTP Request
 UTL_HTTP.WRITE_TEXT(REQ, P_CONTENT);
 RES := UTL_HTTP.GET_RESPONSE(REQ);

 -- Get The Response From The HTTP Call
 BEGIN
   LOOP
     UTL_HTTP.READ_LINE(RES, BUFFER);
     RESPONS := RESPONS || BUFFER;
   END LOOP;
   UTL_HTTP.END_RESPONSE(RES);
   RETURN RESPONS;
 EXCEPTION
   WHEN UTL_HTTP.END_OF_BODY THEN UTL_HTTP.END_RESPONSE(RES);
   RETURN RESPONS;
 END;
END;

如下所示

SELECT
SEND_DB_REQUST (
                'https://gw-fatoora.zatca.gov.sa/e-invoicing/developer-portal/compliance',
                '"csr": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tL1WVh3eUxUSXpOSHd6TFRNMU5ERWYKTUIwR0NnbVNKb21UOGl4a0FRRU1Eek14TURFM05UTTVOelF3TURBd016RU5NQXNHQTFVRURBd0VNVEV3TURFUQpNQTRHQTFVRUdnd0hXbUYwWTJFZ016RVlNQllHQTFVRUR3d1BSbTl2WkNCQ2RYTnphVzVsYzNNek1Bb0dDQ3FHClNNNDlCQU1DQTBjQU1FUUNJQ3JyTzdtSzZWZTZNTmIrSlNJRkRmK0FGMjhqV2ZJYTNIdzlhWEdVOS9KbkFpQXIKSnBVc0h4Z1RrOGtQZTRQSnNJVGJJYXlTeUh2emZwdHFFTWZEajdQN2F3PT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUgUkVRVUVTVC0tLS0t"'
                )
FROM DUAL;

注意:用户应该被授予执行utl_http包的权限

相关问题