pallavi tade
pallavi tade

Reputation: 11

export data from service now rest API using python

I have to export incident data from service now rest API. The incident state is one of new, in progress, pending not resolved and closed. I am able to fetch data that are in active state but not able to apply correct filter also in output it is showing one extra character 'b', so how to remove that extra character?

input:

import requests

URL = 'https://instance_name.service-now.com/incident.do?CSV&sysparm_query=active=true'

user = 'user_name'

password = 'password'

headers = {"Accept": "application/xml"}

response = requests.get(URL,auth=(user, password), headers=headers)

if response.status_code != 200: print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.content) exit()

print(response.content.splitlines())

Output: [b'"number","short_description","state"', b'"INC0010001","Test incident creation through REST","New"', b'"INC0010002","incident creation","Closed"', b'"INC0010004","test","In Progress"']

Upvotes: 0

Views: 2148

Answers (1)

Sunil Bhati
Sunil Bhati

Reputation: 16

It's a Byte literal (for more info. please refer https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals)

To remove Byte literal, we need to decode the strings, Just give a try as-

new_list = []
s = [b'"number","short_description","state"', b'"INC0010001","Test 
incident creation through REST","New"', b'"INC0010002","incident 
creation","Closed"', b'"INC0010004","test","In Progress"']
for ele in s:
    ele= ele.decode("utf-8")
    new_list.append(ele)
print(new_list)

Output:

['"number","short_description","state"', '"INC0010001","Test incident 
creation through REST","New"', '"INC0010002","incid
ent creation","Closed"', '"INC0010004","test","In Progress"']

Hope! It will work

Upvotes: 0

Related Questions