REST API w/Python for SENDING PUT/POST for assigning ticket to myself

DavideT
Tera Contributor

Hello everyone,

 

I am new to the community, sorry if I mismatched the board.

 

I am new to snow api environment. I am currently working on getting ticket table via get (python requests).

 

I wonder if you can write just a sample snippet for assigning incident number xxx to myself via API.

 

Thank you a lot in advance.

 

Davide

9 REPLIES 9

You error says it all.    You are not passing a proper JSON object.    try this instead:

data = json.dumps({ "assigned_to": "your_sys_id"})

 

DavideT
Tera Contributor

Update:

headers = {"Content-Type":"application/json","Accept":"application/json"}
data = {'assigned_to':'12axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa'}

response = requests.put(url, auth=(user, pwd), headers=headers, json=data, timeout=None)

 

now I get 200 but incident is not assigned to me, state doesn't turn into 'in progress'.

 

Could you help me wiath that?

DavideT
Tera Contributor

Update

 

headers = {"Content-Type":"application/json","Accept":"application/json"}
data = {'assigned_to':'12cxxxxxxxxxxxxxxxxxxxxxxxxxa'}

response = requests.put(url, auth=(user, pwd), headers=headers, json=data, timeout=None)

returns status 200 but still no change in ticket state (it remains assigned with u_presa_in_carico_on_hold = false)

 

Please help

 

Thanks a lot

Amit Gujarathi
Giga Sage
Giga Sage

HI @DavideT ,
I trust you are doing great.

Please refer the below code for the same.

import requests
from requests.auth import HTTPBasicAuth

# ServiceNow instance details
instance_url = 'https://your_instance.service-now.com'
api_endpoint = '/api/now/table/incident/'
incident_number = 'INC0012345'  # Replace with your incident number
user_sys_id = 'your_sys_id'  # Replace with your user sys_id

# Authentication details
username = 'your_username'
password = 'your_password'

# Headers
headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}

# Data to update the incident
data = {
    'assigned_to': user_sys_id
}

# Construct URL
url = f'{instance_url}{api_endpoint}{incident_number}'

# Make the PUT request
response = requests.put(url, auth=HTTPBasicAuth(username, password), headers=headers, json=data)

# Check the response
if response.status_code == 200:
    print('Incident assigned successfully.')
else:
    print('Failed to assign incident:', response.content)

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Dear Amit,

 

thank you but this is not working!

 

 

 

check_assign_to_me.png

 

In fact, you can see above the result of the code you posted (that I tried by myself as well before).

 

What I need is to 'click' on the 'Assign to me' button so that the incident will CHANGE STATE as it follows:

 

check_in_progress.png

 

I thank you but I still need help. Maybe the topic was not clear at the start and I apologize for that. Please help!

 

Thank you.

Davide