0

I am trying to calling to call API from PostgreSQL function using PL/Python. When I call API it raised error and I searching from last two days to resolve the error but no success. Below is my function in PostgreSQL

CREATE OR REPLACE FUNCTION call_api(url TEXT, method TEXT, data TEXT) RETURNS JSON AS $$
import urllib.request
import json

req = urllib.request.Request(url)
req.method = method
req.add_header('Content-Type', 'application/json')
if data:
    data = data.encode('utf-8')
    req.data = data

response = urllib.request.urlopen(req)
json_response = json.loads(response.read())

return json_response
$$ LANGUAGE plpython3u;

Below is call to function in PostgreSQL

SELECT public.call_api('https://localhost:7275/api/Employee', 'GET','{"key": "value"}')

Below is error when I call above function

ERROR:  ModuleNotFoundError: No module named '_socket'
CONTEXT:  Traceback (most recent call last):
  PL/Python function "call_api", line 2, in <module>
    import urllib.request

I am using PostgreSQL 15.0 and python version Python-3.10. I don't know what's wrong

user1891251
  • 41
  • 1
  • 1
  • 12

0 Answers0