You are not logged in.
Pages: 1
Hi guys, I'm tryung to create a ticket in GLPI, using python requests. My code:
from glpi import GLPI
import requests
url = '********'
auth = ('username', 'password')
headers = {'App-Token': 'token',
'Content-Type': 'application/json'}
requests.get(url, auth=auth, headers=headers)
payload = {'input': {'name': 'Проблема', 'content': 'Не работает компьютер', 'entities_id': 29}}
requests.post('********', json=payload)
But the ticket is not creating, and there is no exception in my script.
Process finished with exit code 0
What I'm doing wrong? Someone can help me?
And should I use encoding to utf-8 to russian text?
Last edited by random_box (2023-03-22 21:27:14)
Offline
With the initsession you should get à session token.
This token is passed in the header for the post request
Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1 Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6
Offline
Now I'm getting a session token. But when I try to post it in headers, I have an exception:
import json
import requests
# init session
init_url = '********'
auth = ('user', 'password')
init_headers = {
'Content-Type': 'application/json',
'App-Token': '********',
}
login = requests.get(init_url, auth=auth, headers=init_headers)
# get session token
data = login.json()
session_token = json.dumps(data['session_token'])
print(session_token)
# creating a ticket
ticket_url = '********'
ticket_headers = {
'Content-Type': 'application/json',
'App-Token': '********',
'Session-Token': session_token}
payload = {
'Content-Type': 'application/json',
'input': {'name': 'test', 'content': 'test', 'entities_id': 29}
}
create = requests.post(ticket_url, headers=ticket_headers, params=payload)
print(create.text)
# kill session
kill_url = '********'
kill_headers = {
'Content-Type': 'application/json',
'App-Token': '********',
'Session-Token': session_token
}
r_kill = requests.get(kill_url, headers=kill_headers)
print(r_kill.text)
------------------------------------------------------------------------
"bqq70i7318jdn80cbg8ce1uta1"
["ERROR_SESSION_TOKEN_INVALID","session_token возможно недействительный"]
["ERROR_SESSION_TOKEN_INVALID","session_token возможно недействительный"]
Process finished with exit code 0
Last edited by random_box (2023-03-22 21:26:33)
Offline
Don't convert session_token to str with dumps. Here you go;
try:
response = requests.get(
url="{}{}".format(base_url,init_uri),
params={
"app_token": app_token,
"user_token": user_token
}
)
print('Response HTTP Status Code: {status_code}'.format(status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(content=response.content))
resp_json = response.json()
session_token = resp_json['session_token']
print(session_token)
headers = {"Session-Token":session_token, "App-Token":app_token, "Content-Type": "application/json"}
ticket_input = {"input": {"name": "TEST-TICKET4", "content": "DESCS"}}
post_ticket = requests.post(url="{}{}".format(base_url,ticket_uri), headers=headers, data=json.dumps(ticket_input))
print(post_ticket.content)
except requests.exceptions.RequestException as err:
print('HTTP Request failed: ', err)
Offline
Pages: 1