2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ServiceNow - PowerShellから日本語を含むデータをPOSTする

Posted at

PowerShellからServiceNow APIに日本語を含むデータをPOSTしても正しく処理されない場合がある。ServiceNow APIはデータがUTF-8でエンコーディングされていることを前提としているため他エンコーディングされていると正しく処理しない。
Linux上でPowerShellを利用している場合はデフォルトでUTF-8になりますが、WindowsでPowerShellを利用するとUTF-8ではない。
その場合は次のようにPOSTする文字列をバイナリで送信する。

$user = "<ServiceNowログインユーザ名>"
$pass = "<ServiceNowログインパスワード>"
$instanceName = "<ServiceNowインスタンス名>"
$tableName = "<データを挿入するテーブル名>"

# 投稿先URL
$uri = " https://" + $instanceName + ".service-now.com/api/now/table/" + $tableName + "?sysparm_display_value=true"
$method = "post"

# 挿入するデータ
$body = "{`"u_user_id`":`"イベント2`"}"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($body)

# 認証情報の設定
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# ヘッダの設定
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')

# 投稿
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $bytes

以上

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?