Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

$bytes = [System.Text.Encoding]::UTF8.GetBytes($body)

 

I have tried this and can confirm my success.

I didn't realize it was a PowerShell-only issue.
I am glad to know this this time.

Thank you very much.

Hitoshi Ozawa
Giga Sage
Giga Sage

Koki,

Ankur's advise on setting "sysparm_display_value=true" worked in my instance. I was able to insert a reference field into my table.