{"id":271,"date":"2019-09-04T16:56:39","date_gmt":"2019-09-04T14:56:39","guid":{"rendered":"http:\/\/anjo.pt\/wp\/keyword-oracle\/?p=271"},"modified":"2020-07-09T09:48:59","modified_gmt":"2020-07-09T07:48:59","slug":"make-a-rest-call-with-json-content-to-a-https-webservice-from-pl-sql","status":"publish","type":"post","link":"https:\/\/anjo.pt\/keyword-oracle\/2019\/09\/04\/make-a-rest-call-with-json-content-to-a-https-webservice-from-pl-sql\/","title":{"rendered":"Make a REST call with JSON content to a HTTPS webservice from PL\/SQL"},"content":{"rendered":"\n<p>All keywords are now in the title. \ud83d\ude42<\/p>\n\n\n\n<p>I&#8217;ve started developing this small PL\/SQL procedure based on Lucas Jelema blog entry  <a rel=\"noreferrer noopener\" aria-label=\"Invoke a REST service from PL\/SQL \u2013 make an HTTP POST request using UTL_HTTP in Oracle Database 11g XE (opens in a new tab)\" href=\"https:\/\/technology.amis.nl\/2015\/05\/11\/invoke-a-rest-service-from-plsql-make-an-http-post-request-using-utl_http-in-oracle-database-11g-xe\/\" target=\"_blank\">Invoke a REST service from PL\/SQL \u2013 make an HTTP POST request using UTL_HTTP in Oracle Database 11g XE<\/a> .<\/p>\n\n\n\n<p>Unfortunately it took me a while to find out why I was getting the error:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;!DOCTYPE HTML PUBLIC &quot;-\/\/IETF\/\/DTD HTML 2.0\/\/EN&quot;&gt;\n&amp;lt;html&gt;&amp;lt;head&gt;\n&amp;lt;title&gt;400 Bad Request&amp;lt;\/title&gt;\n&amp;lt;\/head&gt;&amp;lt;body&gt;\n&amp;lt;h1&gt;Bad Request&amp;lt;\/h1&gt;\n&amp;lt;p&gt;Your browser sent a request that this server could not understand.&amp;lt;br \/&gt;\n&amp;lt;\/p&gt;\n&amp;lt;\/body&gt;&amp;lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p>There is a empty space at the beginning of a variable in the example on Lucas&#8217; blog post &#8211; before the HTTP\/1.1 on the line below:<\/p>\n\n\n\n<p><code>  req := utl_http.begin_request(url, 'POST',' HTTP\/1.1'); <\/code><\/p>\n\n\n\n<p>My need is a bit more complex, so I leave here the complete code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Certificate and Oracle wallet<\/h2>\n\n\n\n<p>First you need to download the certificate. A good tutorial do to this is at <a rel=\"noreferrer noopener\" aria-label=\"Shellhacks.com (opens in a new tab)\" href=\"https:\/\/www.shellhacks.com\/get-ssl-certificate-from-server-site-url-export-download\/\" target=\"_blank\">Shellhacks.com<\/a>.<\/p>\n\n\n\n<p>Then you need to create a wallet on the database server and add the certificate:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmkdir ${ORACLE_BASE}\/admin\/${ORACLE_SID}\/wallet\norapki wallet create -wallet ${ORACLE_BASE}\/admin\/${ORACLE_SID}\/wallet -pwd WalletPassword123 -auto_login\norapki wallet add -wallet ${ORACLE_BASE}\/admin\/${ORACLE_SID}\/wallet -trusted_cert -cert &quot;\/tmp\/my_certificate.crt&quot; -pwd WalletPassword123\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Oracle ACLs<\/h2>\n\n\n\n<p>On the database (my case was Oracle 11.2) you need to have XDB installed and add the ACLs. I skip the instructions to install XDB. The user is called GREEN.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ngrant execute on utl_http to GREEN;\ngrant execute on dbms_lock to GREEN;\n\nBEGIN\n  DBMS_NETWORK_ACL_ADMIN.create_acl (\n    acl          =&gt; &#039;acl_file.xml&#039;, \n    description  =&gt; &#039;Allows sending alerts to REST service&#039;,\n    principal    =&gt; &#039;GREEN&#039;,\n    is_grant     =&gt; TRUE, \n    privilege    =&gt; &#039;connect&#039;,\n    start_date   =&gt; SYSTIMESTAMP,\n    end_date     =&gt; NULL);\nend;\n\/\n\nbegin\n  DBMS_NETWORK_ACL_ADMIN.assign_acl (\n    acl         =&gt; &#039;acl_file.xml&#039;,\n    host        =&gt; &#039;my_rest_service.com&#039;, \n    lower_port  =&gt; 443,\n    upper_port  =&gt; NULL);    \nend; \n\/\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Procedure to invoke REST service<\/h2>\n\n\n\n<p>The rest service requires a username password. I use the encoded version, so there are no visible passwords in the code. You can encode the pair username:password <a href=\"https:\/\/www.blitter.se\/utils\/basic-authentication-header-generator\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE OR REPLACE PROCEDURE green.publish_event\n( p_robot in varchar2, p_business_service in varchar2, p_target_ci in varchar2, p_transaction in varchar2, p_status in number, p_message in varchar2) is\n  req utl_http.req;\n  res utl_http.resp;\n  url varchar2(4000) := &#039;https:\/\/my_rest_service.com\/api\/event&#039;;\n  buffer varchar2(32767); \n  content varchar2(8000) := &#039;{ &quot;robot&quot;:&quot;&#039;||p_robot||&#039;&quot;, &quot;business_service&quot;:&quot;&#039;||p_business_service||&#039;&quot;, &quot;target_ci&quot;:&quot;&#039;||p_target_ci||&#039;&quot;, &quot;transaction&quot;:&quot;&#039;||p_transaction||&#039;&quot;, &quot;status&quot;:&quot;&#039;||p_status||&#039;&quot;, &quot;message&quot;:&quot;&#039;||p_message||&#039;&quot;, &quot;target_support_group&quot;: &quot;&quot;, &quot;organization&quot;: &quot;ANJO_EVENT&quot;}&#039;;\n  \nBEGIN\n  utl_http.set_wallet(&#039;file:${ORACLE_BASE}\/admin\/${ORACLE_SID}\/wallet&#039;, null);\n\treq  :=UTL_HTTP.begin_request(url, &#039;POST&#039;);\n\tutl_http.set_header(req, &#039;Authorization&#039;, &#039;Basic f24iYsBpdGVzdDp3ZWJhcGl0ZXN0&#039;); \n  utl_http.set_header(req, &#039;Content-Length&#039;, length(content));\n  utl_http.write_text(req, content);\n\n  res := utl_http.get_response(req);\n  -- process the response from the HTTP call\n  begin\n    loop\n      utl_http.read_text(res, buffer,32767);\n      dbms_output.put_line(buffer);\n    end loop;\n    utl_http.end_response(res);\n  exception\n    when utl_http.end_of_body \n    then\n      utl_http.end_response(res);\n  end;\nEND publish_event;\n\/\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Trigger to automatically send event<\/h2>\n\n\n\n<p>In my case, the idea is to send an event every time a line is inserted into a table. So I just create a small trigger that calls the procedure above.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE OR REPLACE TRIGGER green.trg_table1_monitor\nAFTER INSERT\n   ON green.table1_monitor\n   FOR EACH ROW\nBEGIN\n   publish_event (\n\t   p_robot =&gt; :new.host_name,\n\t\t p_business_service =&gt; &#039;IT DB&#039;,\n\t\t p_target_ci =&gt; &#039;356224bs5b1f6f401d2262eb0b9619ad&#039;,\n\t\t p_transaction =&gt; &#039;Table1&#039;,\n\t\t p_status =&gt; 7,\n\t\t p_message =&gt; &#039;Green-Table1 -Exception: &#039;|| substr(:new.exception,0,100) \n\t  );\nEND;\n\/\n<\/pre><\/div>\n\n\n<p>Et voil\u00e0!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All keywords are now in the title. \ud83d\ude42 I&#8217;ve started developing this small PL\/SQL procedure based on Lucas Jelema blog entry Invoke a REST service from PL\/SQL \u2013 make an HTTP POST request using UTL_HTTP in Oracle Database 11g XE . Unfortunately it took me a while to find out why I was getting the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47,46],"tags":[],"class_list":{"0":"post-271","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-json","7":"category-rest","8":"czr-hentry"},"_links":{"self":[{"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":2,"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/posts\/271\/revisions\/378"}],"wp:attachment":[{"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anjo.pt\/keyword-oracle\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}