Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling RESTful Web Services from PostgreSQL procedure/function

I have been provided RESTful web services to push data into a remote DB of another application. I need to call these services to push data from PostgreSQL DB by sending data in JSON format as GET/POST parameters to the web service. Is it possible to call these web services from the PostgreSQL functions (periodically) which push data into my database in the first place, or write JAVA code to call these web services that run queries on PostgreSQL database and call web services to pass them to the remote DB.

like image 944
S_S Avatar asked Oct 03 '17 08:10

S_S


People also ask

Can we call API from PostgreSQL?

PostgreSQL REST API connection allows to create Restful applications with PostgreSQL as a database. A REST API (also known as RESTful API) is an application programming interface (API or web API) . The API follows the constraints of REST architectural style and allows for interaction with RESTful web services.

Does Postgres support REST API?

PostgREST is a standalone web server that turns your PostgreSQL database directly into a RESTful API. The structural constraints and permissions in the database determine the API endpoints and operations.

Can we call stored procedure from function in PostgreSQL?

PostgreSQL does not support true stored procedures (multiple result sets, autonomous transactions, and all that) though, only sql-callable user-defined functions.

What is PostgREST API?

PostgREST is a webserver which magically exposes your PostgreSQL database as an HTTP RESTful API, offering all the CRUD operations – create, read, update and delete. It relies on well known PostgreSQL features.


2 Answers

Yes, it is possible, althought not directly from Postgresql itself. I don't know about Java but the fastest way is to use plperlu with REST::Client package, e.g.:

CREATE OR REPLACE FUNCTION restful.put(auri character varying, ajson_text text)
 RETURNS text
 LANGUAGE plperlu
 SECURITY DEFINER
AS $function$
  use REST::Client;  
  use Encode qw(encode);
  my $client = REST::Client->new();    
  $client->getUseragent()->proxy( 'https', 'http://some-proxy/' ); # use for proxy authentication
  $client->addHeader('Content-Type', 'application/json');          # headers
  $client->POST( $_[0], encode('UTF-8', $_[1]));                   # encoding
  return $client->responseContent();  
$function$
like image 104
JustMe Avatar answered Sep 19 '22 01:09

JustMe


Using plpython2u language:

Solution 1: (using urllib2)

CREATE OR REPLACE FUNCTION public.py_pgrest(uri text, body text DEFAULT NULL::text, content_type text DEFAULT 'application/json'::text)
 RETURNS text
 LANGUAGE plpython2u
AS $function$
    import urllib2
    from urllib2 import Request, urlopen, URLError, HTTPError
    req = Request(uri)
    if body:
        req.add_data(body)
    if content_type:
        req.add_header('Content-Type', content_type)
    try:
        data = urlopen(req)
    except HTTPError as e:
        return e
    except URLError as e:
        if hasattr(e, 'reason'):
            return e.reason
        elif hasattr(e, 'code'):
            return e.code
        else:
            return e
    else:
        return data.read()
$function$
;

Solution 2: (using requests)

CREATE OR REPLACE FUNCTION public.py_pgrest(p_url text, p_method text DEFAULT 'GET'::text, p_data text DEFAULT ''::text, p_headers text DEFAULT '{"Content-Type": "application/json"}'::text)
 RETURNS text
 LANGUAGE plpython2u
AS $function$
    import requests, json
    try:
        r = requests.request(method=p_method, url=p_url, data=p_data, headers=json.loads(p_headers))
    except Exception as e:
        return e
    else:
        return r.content
$function$
;
like image 26
x3mEn Avatar answered Sep 20 '22 01:09

x3mEn