Forum Stats

  • 3,769,579 Users
  • 2,252,981 Discussions
  • 7,875,092 Comments

Discussions

How to get valid error message while invoking REST API through Python

Praveen Gollu-Oracle
Praveen Gollu-Oracle Member Posts: 45 Employee

Hi Team,

I have written the below script to invoke REST API to update a record in the Cloud system. We are able to connect and update the records successfully for success records. But for the error records we are able to get the error code but unable to get the valid error message for the record create/update failure. How can we get the valid error message for the same. Kindly suggest.

import requests

import json

import csv

import os

user = 'abc'

password = 'abc'

autha = [user,password]

with open('D:\Praveen\Projects\Python\PythonScripts\PjfRBS_LOAD1.csv') as csv_file:

    csv_reader = csv.reader(csv_file, delimiter=',')

    line_count = 0

    for row in csv_reader:

        if line_count == 0:

            print("This is the first line of the file")

            line_count += 1

        else:

            RBSASSIGMENTID = str(row[1])

            #print("RBSASSIGMENTID" + RBSASSIGMENTID)

            PROJECTID = str(row[0])

            #print("PROJECTID" + PROJECTID)

            RESOURCECLASSNAME = row[3]

            #print("RESOURCECLASSNAME" + RESOURCECLASSNAME)

            FORMATNAME = row[4]

            #print("FORMATNAME" + FORMATNAME)

            RESOURCE_NAME = row[5]

            #print("RESOURCE_NAME" + RESOURCE_NAME)

            EMAILADDRESS = row[6]

            #print("EMAILADDRESS" + EMAILADDRESS)

            PROJECTNUMBER = row[2]

            #print("PROJECTNUMBER" + PROJECTNUMBER)

            PERSONID = str(row[7])

            #print("PERSONID" + PERSONID)

            RBS_URL = 'https://ecgy-dev3.fa.ap2.oraclecloud.com/fscmRestApi/resources/11.13.18.05/projectPlanningRbs/'+RBSASSIGMENTID+'/child/Elements'

            print(RBS_URL)

            data = {"ResourceClassName": RESOURCECLASSNAME,"FormatName": FORMATNAME,"PersonId": PERSONID,"ResourceName":RESOURCE_NAME}

            jstr = json.dumps(data,indent=4)

            print(jstr)

            patch_response = requests.post(url = RBS_URL,auth=(user,password),json=jstr)

            if patch_response.status_code == 200:

                print('We are able to connect to PATCH REST API')

                print('Update for the Project = '+ PROJECTNUMBER + ', Project ID = '+ PROJECTID + ', RESOURCE_CLASSNAME ='+ RESOURCECLASSNAME + ', RESOURCE_NAME =' + RESOURCE_NAME + 'and EMAILADDRESS ='+ EMAILADDRESS)

                line_count += 1

            else:

                patch_response.reason

                print('we are not able to connect to PATCH REST API'+patch_response.reason)

                ##print('Update for the Project = '+ PROJECTNUMBER + ', Project ID = '+ PROJECTID + ', RESOURCE_CLASSNAME ='+ RESOURCECLASSNAME + ', RESOURCE_NAME =' + RESOURCE_NAME + 'and EMAILADDRESS ='+ EMAILADDRESS)

    print('file Processed' + str(line_count) + 'lines.')