Python Forum
Calling Oracle REST SQL from Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling Oracle REST SQL from Python
#1
Hello All,

I 'am tying to mimic the below curl in python::

Output:
sh-4.2$ curl -X "POST" "http://slc05ewk.us.oracle.com:8080/ords/hr/_/sql" \ -H "Content-Type: application/sql" \ -u TESTUSER1:testuser1 \ -d $'SELECT sysdate FROM dual;'
**output**::
Output:
{"env":{"defaultTimeZone":"PST8PDT"},"items":[{"statementId":1,"statementType":"query","statementPos":{"startLine":1,"endLine":1},"statementText":"SELECT sysdate FROM dual","resultSet":{"metadata":[{"columnName":"SYSDATE","jsonColumnName":"sysdate","columnTypeName":"DATE","precision":0,"scale":0,"isNullable":1}],"items":[{"sysdate":"2020-11-04T04:56:22Z"}],"hasMore":false,"limit":100,"offset":0,"count":1},"response":[],"result":0}]} sh-4.2$
I tried below with python, but it doesn’t give the above…

# import requests module 
import requests 
from requests.auth import HTTPBasicAuth 
  
# Making a get request 
url = 'http://slc05ewk.us.oracle.com:8080/ords/hr/_/sql'
sql = 'select sysdate from dual;'
response = requests.get(url, auth = HTTPBasicAuth('testuser1', 'testuser1'), data=sql) 
  
# print request object 
print(response.text) 
All i get is below::

Output:
-bash-4.2$ python restSQL_1.py {"database_product_name":"Oracle","database_product_version":"Oracle Database 20c Enterprise Edition Release 20.0.0.0.0 - Development\nVersion 20.1.0.0.0","database_major_version":20,"database_minor_version":0} -bash-4.2$
How do i get it done ?

Thank you.
Regards,
John Jacob
Reply
#2
When you call cURL, you're making a POST request, but in your Python code, you're making a GET request (line 8).
Reply
#3
Thumbs Up 
(Nov-04-2020, 08:45 AM)ndc85430 Wrote: When you call cURL, you're making a POST request, but in your Python code, you're making a GET request (line 8).


ah.... you nailed it. That was it !
Thank you for pointing it out.

Below worked ..

import requests
from requests.auth import HTTPBasicAuth

# Making a get request
url = 'http://slc05ewk.us.oracle.com:8080/ords/hr/_/sql'
sql = 'select sysdate from dual;'
header = {"Content-Type": "application/sql"}
response = requests.post(url, auth = HTTPBasicAuth('testuser1', 'testuser1'), data=sql, headers=header)

# print request object
print(response.text)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python script is hanging while calling a procedure in database prasanthi417 4 442 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 988 Sep-19-2022, 02:32 AM
Last Post: Xeno
  API REST Package for Calling/Flask muzikman 12 4,133 Oct-22-2021, 07:51 PM
Last Post: muzikman
  Encrypting Oracle Passwords / Python Library for That? bmccollum 1 2,528 Jun-11-2021, 07:59 PM
Last Post: Larz60+
  Calling python from c++ in visual studio pdk5 0 2,126 May-24-2021, 10:18 AM
Last Post: pdk5
  Python to Oracle Conn Issue chvsnarayana 2 32,577 Sep-06-2020, 04:33 PM
Last Post: Larz60+
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,681 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 2 6,411 Jul-09-2020, 04:46 PM
Last Post: srikanth7482
  Load table from Oracle to MYSQL using python himupant94 0 1,607 May-12-2020, 04:50 PM
Last Post: himupant94
  Parse a REST API call using Python GKT 1 1,874 May-07-2020, 04:15 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020