Python Forum
requests.get miss format the conecction url
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
requests.get miss format the conecction url
#1
Question 
HI
I am writing some code to use rest API calls to our Service Desk Solution and get information from tickets.
I am using the request module to use the requests.post and requests.get commands.

The code must login, get info and logout

The login and logout calls works ok, the status code always is 200. The API call to get the ticket information returns a 500 code.
reviewing the requests properties i print the url code used in the requests.get call, and I note the request.url missformat the string

I mean, the original url string is:

url = 'http://<ip_server>//cgrestapi/api/entity/incidentrequest/TodosLosIncidentes??filter=[ID]=99'
this string is used in this call

r = req.get(url)

the I print the status code and url (print (r.status_code) & print (r.url) ), the r.url get this string:
http://<ip_server>//cgrestapi/api/entity/incidentrequest/TodosLosIncidentes??filter=%5BID%5D=99

As you note the url string have %5B and %5D, instead of "[" and "]"

Some one have some idea why the string changes in the GET call?

This is the code and the output result (the yellow code is the code with the problem):

import requests as req
from requests.auth import HTTPBasicAuth

jbody={"username":"LumaServiceAccount","password":"admin" }
r = req.post(
     "http://<ip_server>/cgrestapi/api/authenticate/user", 
     json=jbody
)
print('URL Login: ', r.url)
datos = r.json()
print ("Staus re.post-login: ", r.status_code ) 
id = datos["sessionId"]
print ('Ticket get info')

[color=#F1C40F]url = 'http://<ip_server>//cgrestapi/api/entity/incidentrequest/TodosLosIncidentes??filter=[ID]=99'

r = req.get(url)

print('URL r.get: ', r.url)
print ("Staus re.get: ", r.status_code)
[/color]
print('logout')
Headers={"sessionid":id}
r = req.post(
    'http://172.16.10.30/cgrestapi/api/user/logout', headers=Headers
)
print ("Staus re.post -logou: ", r.status_code ) 

Output: 
PS C:\Software\Programs> py testr.py
URL Login:  http://<ip_server>/cgrestapi/api/authenticate/user
Staus re.post-login:  200
Ticket get info
URL r.get:  http://<ip_server>/cgrestapi/api/entity/incidentrequest/TodosLosIncidentes??filter=%5BID%5D=99
Staus re.get:  500
logout
Staus re.post -logou:  200
Larz60+ write Jul-05-2022, 06:54 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
(Jul-04-2022, 03:51 PM)jlcc_py Wrote: HI
I am writing some code to use rest API calls to our Service Desk Solution and get information from tickets.
I am using the request module to use the requests.post and requests.get commands.

The code must login, get info and logout

The login and logout calls works ok, the status code always is 200. The API call to get the ticket information returns a 500 code.
reviewing the requests properties i print the url code used in the requests.get call, and I note the request.url missformat the string

I mean, the original url string is:

url = 'http://<ip_server>//cgrestapi/api/entity/incidentrequest/TodosLosIncidentes??filter=[ID]=99'
this string is used in this call

r = req.get(url)

the I print the status code and url (print (r.status_code) & print (r.url) ), the r.url get this string:
http://<ip_server>//cgrestapi/api/entity/incidentrequest/TodosLosIncidentes??filter=%5BID%5D=99

As you note the url string have %5B and %5D, instead of "[" and "]"

Some one have some idea why the string changes in the GET call?

This is the code and the output result (the yellow code is the code with the problem):

import requests as req
from requests.auth import HTTPBasicAuth

jbody={"username":"LumaServiceAccount","password":"admin" }
r = req.post(
     "http://<ip_server>/cgrestapi/api/authenticate/user", 
     json=jbody
)
print('URL Login: ', r.url)
datos = r.json()
print ("Staus re.post-login: ", r.status_code ) 
id = datos["sessionId"]
print ('Ticket get info')

[color=#F1C40F]url = 'http://<ip_server>//cgrestapi/api/entity/incidentrequest/TodosLosIncidentes??filter=[ID]=99'

r = req.get(url)

print('URL r.get: ', r.url)
print ("Staus re.get: ", r.status_code)
[/color]
print('logout')
Headers={"sessionid":id}
r = req.post(
    'http://172.16.10.30/cgrestapi/api/user/logout', headers=Headers
)
print ("Staus re.post -logou: ", r.status_code ) 

Output: 
PS C:\Software\Programs> py testr.py
URL Login:  http://<ip_server>/cgrestapi/api/authenticate/user
Staus re.post-login:  200
Ticket get info
URL r.get:  http://<ip_server>/cgrestapi/api/entity/incidentrequest/TodosLosIncidentes??filter=%5BID%5D=99
Staus re.get:  500
logout
Staus re.post -logou:  200

i fix the error,
The PUT call needs a SessionId, adding this as "header", after add the header="sessionID": "dchdhdch32333233"
the call returns a 200 code
Reply
#3
I am writing some code to use REST API calls to retrieve ticket information from our Service Desk Solution.
I am utilizing the requests using the request module.
post and requests.
get commands. flagle
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  POST requests - different requests return the same response Default_001 3 1,938 Mar-10-2022, 11:26 PM
Last Post: Default_001

Forum Jump:

User Panel Messages

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