简体   繁体   中英

Service Now API comment breaks with multiple line comment

I am making this call to make comments to service now:

url = 'https://myinstance.service- 
now.com/api/now/table/incident/' +  sys_id

user = 'user'
pwd = 'pass'

headers = {"Content- 
Type":"application/json","Accept":"application/json"}

# Do the HTTP request
response = requests.patch(url, auth=(user, pwd), headers=headers ,data="{\"work_notes\":\"" +  comment + "\"}")

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

And this works when the comment variable is just one line, but with multiple lines it breaks, I removed the \\n from string and it does work. So how do I process this request with multiple lines, I cant get rid of the spaces the comments have to look well formatted. The comment is coming from zendesk api.

The print statement after the if not status 200 prints as following with multiple lines:

this
is
a
test

It was pretty simple, had to turn that part of the request into a JSON like this:

data = {}
data['work_notes'] = comment
comment = json.dumps(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM