Home Calling Rest api using pl/sql
Reply: 0

Calling Rest api using pl/sql

Mike L.
1#
Mike L. Published in 2017-12-07 21:16:48Z

So I am trying to learn Oracle. So am new to it, take it easy on me. My question is this, how can i call rest api using pl/sql.

So i found this site http://jsonplaceholder.typicode.com/posts. If put it restClient for firefox, POST method, i get this in response:

{
  "id": 101
}

So now i want to get id:101 and display it in SQL Developer. I found this code below. But i cant get it to work. My question is where to put the id. So when i start the process i get for ID value 101. Can someone please explain and write some example. I know that i have the example below but i just don't understand i need a working example to figure it out.

EDITED :

create or replace procedure call_rest_webservice
( p_id out varchar2
) AS 

BEGIN
          DECLARE
            t_http_req               utl_http.req;
            t_http_resp              utl_http.resp;
            req_body                 VARCHAR2(200);
            buff                     VARCHAR2(32767);

begin
  req_body := '{"id":"'|| p_id ||'"}';
  t_http_req:= utl_http.begin_request( url=> 'http://jsonplaceholder.typicode.com/posts/1', method=>'POST' );
  utl_http.SET_HEADER (r      =>  t_http_req, 
                                 name   =>  'Content-Type',   
                                 value  =>  'application/json');
  UTL_HTTP.SET_HEADER (r      =>   t_http_req, 
                                 name   =>   'Content-Length', 
                                 value  =>  LENGTHB(req_body));

 utl_http.write_text(t_http_req, req_body);
            t_http_resp:= utl_http.get_response(t_http_req);
              -- END CREATING REQUEST

            UTL_HTTP.READ_TEXT(t_http_resp, buff, LENGTH(buff));
            utl_http.end_response(t_http_resp);

      END;
END call_rest_webservice;

When i put the url http://jsonplaceholder.typicode.com/posts/1 in firefox i get :

userId  1
id  1
title   "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
body    "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302989 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO