Python Forum

Full Version: requests.get miss format the conecction url
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
(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
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