Python Forum
How to get valid error message while invoking REST API through Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get valid error message while invoking REST API through Python
#1
Hi Experts,

I am new to python. Have written code to invoke REST API performing poST operation. I am getting 400 Bad Request ERROR. When I try running the same data using SOAP UI tool I am getting 400 error but displaying valid error message.

How can get the valid error message through python scripting. Please find the below python script for oyur reference.

import requests
import json
import csv
import os
import pdb

user = 'abc'
password = 'abc'
autha = [user,password]

with open('D:\User\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://CLOUD/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:
                ##pdb.set_trace()
                patch_response.reason
                print('we are not able to connect to PATCH REST API '+ patch_response.reason + str(patch_response.status_code))
                ##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.')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is this a valid variable name? Skaperen 6 465 Jun-05-2024, 10:13 PM
Last Post: Skaperen
  Script getting reindexing only valid error cubangt 1 1,370 Dec-07-2023, 04:06 PM
Last Post: cubangt
  invoking python in Terminal Euler 2 735 Aug-25-2023, 06:17 AM
Last Post: perfringo
  Error message about iid from RandomizedSearchCV Visiting 2 1,184 Aug-17-2023, 07:53 PM
Last Post: Visiting
  Another Error message. the_jl_zone 2 1,080 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
Question Use function, retry until valid Ashcora 8 1,721 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,868 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  understanding error message krlosbatist 1 2,009 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  checking for valid hexadecimal digits Skaperen 3 6,741 Sep-02-2021, 07:22 AM
Last Post: buran
  Error message pybits 1 53,656 May-29-2021, 10:26 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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