Table API query fields with UTF-8 characters

ponq
Kilo Contributor

Hi ServiceNow community,

Im struggling with what would look like a simple problem to solve. I'm creating a record with UTF-8 character included (example the curly quote ’). The record gets created fine but I'm unable to query it anymore. 

Example:

curl --request POST \
  --url https://<instance>.service-now.com/api/now/table/sn_ti_observable \
....
  --data '{
	"value": "aaa’"
}

{
	"result": {
....
		"sys_id": "c4d3f9b487429d502413b919cebb3584",
....
		"value": "aaa’",
....
	}
}

 The record gets created fine and i can query it via sys_id:

curl --request GET \
  --url 'https://<instance>.service-now.com/api/now/table/sn_ti_observable/c4d3f9b487429d502413b919cebb3584' \
'''

{
	"result": {
		"type.name": "",
		"type.value": "",
		"value": "aaa’"
	}
}

Notice the curly single quote is there.

But I'm unable to query the record with value in sysparm_query:

curl --request GET \
  --url 'https://<instance>.service-now.com/api/now/table/sn_ti_observable?sysparm_query=value%3Daaa%E2%80%99&sysparm_fields=name%2C%20value%2C%20type.name%2C%20type.value&sysparm_limit=10' \

{
	"result": []
}

Any hints how the query should be constructed to get the record back?

2 REPLIES 2

Arav
Tera Guru
Tera Guru

Hi,

The term aaa' is getting translated as aaa%27 in my case and I am able to get results when I use the command below.

curl "<my instance>/api/now/table/incident?sysparm_query=short_description%3Daaa%27&sysparm_fields=number,short_description"

It also works when I use the character directly.

curl "<my instance>/api/now/table/incident?sysparm_query=short_description%3Daaa'&sysparm_fields=number,short_description"

Maybe try with the syntax above and check.

Thanks,

Arav

ponq
Kilo Contributor

Nope, when I POST the curly quote gets into the record. Try this python:

import json
import requests

url = "https://<instance>.service-now.com"

payload = {"short_description": "Curly’"}

headers = {
    "Content-Type": "application/json",
    "Authorization": "Basic <token>",
}
response = requests.post(
    f"{url}/api/now/table/sn_si_incident",
    params="sysparm_fields=sys_id,short_description",
    data=json.dumps(payload),
    headers=headers,
)

# display python object
print("Created record")
print(response.json())
# dump to json, non-ascii characters will be visible
print(json.dumps(response.json()))

rj = response.json()["result"]
sys_id = rj["sys_id"]
short_description = rj["short_description"]

response = requests.get(
    f"{url}/api/now/table/sn_si_incident/{sys_id}",
    params={"sysparm_fields": "sys_id,short_description"},
    data=json.dumps(payload),
    headers=headers,
)

# display python object
print("Fetched by sys_id")
print(response.json())
# dump to json, non-ascii characters will be visible
print(json.dumps(response.json()))

response = requests.get(
    f"{url}/api/now/table/sn_si_incident",
    params={"sysparm_fields": "sys_id,short_description", "short_description": short_description},
    data=json.dumps(payload),
    headers=headers,
)

# display python object
print("Fetched by short_description")
print(response.json())
# dump to json, non-ascii characters will be visible
print(json.dumps(response.json()))

here's the result:

Created record
{'result': {'sys_id': '1ab2323097c25190d42b3d121153af9d', 'short_description': 'Curly’'}}
{"result": {"sys_id": "1ab2323097c25190d42b3d121153af9d", "short_description": "Curly\u2019"}}
Fetched by sys_id
{'result': {'sys_id': '1ab2323097c25190d42b3d121153af9d', 'short_description': 'Curly’'}}
{"result": {"sys_id": "1ab2323097c25190d42b3d121153af9d", "short_description": "Curly\u2019"}}
Fetched by short_description
{'result': []}
{"result": []}