About "POST" in Table API

Koki
Tera Contributor

Hi,

I need to get to grips with ServiceNow's Table API.

In order to better understand, I have created and validated PowerShell code.

In the PowerShell code, if I set up the following, adding a record to ServiceNow is successful. (Sample ①)
The "u_kansen_key" field is also populated with a value.

("u_kansen_key" field is a reference type field.)

----------------------------------------------------------------------------------------------------------------------------------------------------

Sample ①

     ~ The endpoint uri, etc. is written here.

# Specify HTTP method
$method = "post"

# Specify request body
$body = "{`"u_kansen_key`":`"TEST`",`"u_mind_kanri_no`":`"testInsert07`"}"

# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

However, if I do the following, the record is added, but the "u_kansen_key" field is empty.
("u_kansen_key" field is a reference type field.)

----------------------------------------------------------------------------------------------------------------------------------------------------

Sample ②

     ~ The endpoint uri, etc. is written here.

# Specify HTTP method
$method = "post"

# Specify request body
$body = "{`"u_kansen_key`":`"Mr.IP 東北`",`"u_mind_kanri_no`":`"testInsert07`"}"

# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

Why is there no value in the "u_kansen_key" field in the case of sample ②?

Does anyone know how I can succeed?

 

 

1 ACCEPTED SOLUTION

Hi again,

Was able to send Japanese key and save it in ServiceNow table with reference using PowerShell 

It's a problem with PowerShell. It's necessary to encode the data as utf8 and send it as bytes as below.

# Specify HTTP method
$method = "post"

# Specify request body
$body = "{`"u_kansen_key`":`"Mr.IP 東北`",`"u_mind_kanri_no`":`"testInsert07`"}"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($body)

# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $bytes

View solution in original post

11 REPLIES 11

I guess I did not ask the right questions.
Sorry.

I had already done "sysparm_display_value=true".
(BTW, I had already tried "sysparm_display_value=all" as well.)

What I was wondering and wanted to ask is why it sometimes succeeds and sometimes fails.

As shown in "Sample ①", it succeeded when I set the input value as "TEST".
As shown in "Sample ②", it fails when the input value is "Mr.IP Northeast".
In both cases, the input values are definitely the display values already registered in the referenced table.


I tried several times after that, and I guess that it fails if the input value is Japanese.
I have not succeeded yet.

Hi,

it should work all the time if you are sending the correct display value

For example: for incident table if you send correct INC number it would set it in reference field

Definitely it would work for English and I doubt it would work for other language such as Japanese etc

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Koki,

Tested on my instance and it works even with Japanese.

Try testing from REST API Explorer.

find_real_file.png

Result:

find_real_file.png

Table definition.

find_real_file.png

I also tested it from curl command on Ubuntu and it worked too.

If using Windows, it may be that the Japanese characters are not being send in UTF8 encoding.

BTW, sysparm_display_value=true isn't supported in Scripted REST API so it was necessary to implemented it myself.

Tried it with Powershell and it doesn't work. It seems to a problem with Powershell.

Hi again,

Was able to send Japanese key and save it in ServiceNow table with reference using PowerShell 

It's a problem with PowerShell. It's necessary to encode the data as utf8 and send it as bytes as below.

# Specify HTTP method
$method = "post"

# Specify request body
$body = "{`"u_kansen_key`":`"Mr.IP 東北`",`"u_mind_kanri_no`":`"testInsert07`"}"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($body)

# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $bytes