从 PostgreSQL 过程/函数调用 RESTful Web 服务 [英] Calling RESTful Web Services from PostgreSQL procedure/function

查看:114
本文介绍了从 PostgreSQL 过程/函数调用 RESTful Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得了 RESTful Web 服务,可以将数据推送到另一个应用程序的远程数据库中.我需要通过将 JSON 格式的数据作为 GET/POST 参数发送到 Web 服务来调用这些服务以从 PostgreSQL 数据库推送数据.是否可以从首先将数据推送到我的数据库的 PostgreSQL 函数(定期)调用这些 Web 服务,或者编写 JAVA 代码来调用这些在 PostgreSQL 数据库上运行查询的 Web 服务并调用 Web 服务将它们传递给远程数据库.

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.

推荐答案

是的,这是可能的,虽然不是直接来自 Postgresql 本身.我不了解 Java,但最快的方法是将 plperluREST::Client 包一起使用,例如:

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$

这篇关于从 PostgreSQL 过程/函数调用 RESTful Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆