Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.9K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 400 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
Python Scripts to Invoke Oracle Cloud REST API's

Hi Experts,
I am new to Python. I am trying to invoke Oracle Cloud REST API's for integrations. Any experts who can through some light the below
1. Prerequisites for invoking REST API's (Library's to be Imported )
2. Authentication prerequisites
3. Sample scripts for invoking REST API's
Any information on this are highly appreciated.
Thanks,
Praveen Gollu
Best Answer
-
Hello,
requests is enough for me
You can go thru Basic authorization(auth=(user,pass)) or use something different like oauth2 etc. by putting of token into header.
for instance:
import requests
response = requests.get(url, [headers=headers], [auth=(user,pass)], [params=params])
response.status_code
response.json()
Answers
-
Hello,
requests is enough for me
You can go thru Basic authorization(auth=(user,pass)) or use something different like oauth2 etc. by putting of token into header.
for instance:
import requests
response = requests.get(url, [headers=headers], [auth=(user,pass)], [params=params])
response.status_code
response.json()
-
Hi Dmytro,
Thanks for the information. As suggested, I tried below logic. Facing the issue.
-------------------------------------------------------------------------------------------------------------------------------------------
import requests
url = 'http://abcd-1234.xyz.oraclecloud.com/fscmRestApi/resources/11.13.18.05/projects' -- Dummy url in the post
user = 'abc.xyz' -- Dummy username in the post
password = 'Welcome' -- Dummy password in the post
auth = [user,password]
response = requests.get(url,auth)
if response.status_code == 200:
print('We are able to connect to REST API')
else:
print('we are not able to connect to REST API')
-------------------------------------------------------------------------------------------------------------------------------------------
I am facing below error. Any suggestions on the same are highly appreciated.
Traceback (most recent call last):
File "D:\Praveen\Projects\Python\PythonScripts\REST_API.py", line 7, in <module>
response = requests.get(url,auth)
File "D:\Praveen\Projects\Python\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "D:\Praveen\Projects\Python\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "D:\Praveen\Projects\Python\lib\site-packages\requests\sessions.py", line 519, in requestprep = self.prepare_request(req)
File "D:\Praveen\Projects\Python\lib\site-packages\requests\sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 313, in prepare
self.prepare_url(url, params)
File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 431, in prepare_url
enc_params = self._encode_params(params)
File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 97, in _encode_params
for k, vs in to_key_val_list(data):
ValueError: too many values to unpack (expected 2)[Finished in 0.796s]
-
this line is incorrect:
response = requests.get(url,auth)
You need write it as:
response = requests.get(url,auth=auth)
url is first mandatory parameter, others - non-mandatory (**kwargs) but you need to specify for what parameter(e.g. auth or headers) you are putting your variable (auth list).
p.s. according to docs, auth should be tuple but you are using list. Just to keep things easier and avoid some issues, plz modify this part as well.
If you behind the proxy, take a look here:
-
Hi Experts,
Thanks for your support and help. I am able to successfully invoke the REST API using Python script. Hope this will be helpful for the new developers to the python.
Once again thanks for all your support.
import requests
import jsonurl = '<SERVER URL>/fscmRestApi/resources/11.13.18.05/projects'
user = 'USERNAME'
password = 'PASSWORD'
auth = [user,password]
##response = requests.get(url,auth = (user,password))
response = requests.get(url, auth = (user,password))
if response.status_code == 200:
print('We are able to connect to GET REST API')
else:
print('we are not able to connect to GET REST API')data = response.text
parsed=json.loads(data)
for product in parsed['items']:
project_name = product['ProjectName']
project_description = product['ProjectDescription']
project_id = str(product['ProjectId'])
project_data = {'ProjectDescription': project_description + 'RESTTEST'}
project_url = '<SERVERURL>/fscmRestApi/resources/11.13.18.05/projects' + '/' + project_id
print(project_name)
patch_response = requests.patch(url = project_url,auth=(user,password),json = {'ProjectDescription': project_description + 'RESTTEST'})
if patch_response.status_code == 200:
print('We are able to connect to PATCH REST API')
else:
print('we are not able to connect to PATCH REST API') -
I have a similar requirement. I need to download a file from UCM. How can we specify the filename on the request? Also when I call this by response=requests.get(url, auth=(user, password)) and try response.text, it gives me a javascript error,
Note, my python IDE has been able to download response from other sources, with same requests library.
-
I think the following links will serve the purpose for you.
Regards,
Pratyush