2

这是我在 Stackoverflow 上的第一篇文章,如果我的文章格式不正确或提交不正确等,请告诉我。我正在尝试使用 powershell 连接到我的 ServiceNow 实例并发布服务目录请求。但是,我收到错误(400)错误请求:


Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:37 char:13
+ $response = Invoke-RestMethod -Headers $headers -Method Post -Uri $ur ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


到目前为止,我已经能够使用 URI 通过 ServiceNow REST API Explorer 成功发布服务目录请求:

POST https://myinstance.service-now.com/api/sn_sc/v1/servicecatalog/items/3a05b357db2443009906540

​​adc961945/order_now原始响应正文为:

{"sysparm_quantity":"1","variables":{"requested_for":"Guest","Request_Type":"New Account","website_login":"2437ffdbdb2443009906540​​adc96198c"}}

我也有能够使用 Powershell 成功将事件发布到 ServiceNow。考虑到这一点,我相信我没有任何访问问题,而是代码的 $body 部分存在语法/格式问题。任何帮助将不胜感激!


# Eg. User name="admin", Password="admin" for this code sample.
$user = "user"
$pass = "pass"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers
$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')

# Specify endpoint uri
$uri = "https://myinstance.service-now.com/api/sn_sc/servicecatalog/items/3a05b357db2443009906540adc961945/order_now"

# Variables
$RequestedFor = "Guest"
$RequestType = "New Account"
$Vendor1 = "2437ffdbdb2443009906540adc96198c"

# Specify request body
$body = @{
sysparm_quantity = "1"
Requested_For = $RequestedFor
Request_Type = $RequestType
website_login = $Vendor1
}

$bodyJson = $body | ConvertTo-Json;

#Convert to UTF8 array to support special chars such as the danish "�","�","�"
$utf8Bytes = [System.Text.Encoding]::UTf8.GetBytes($bodyJson)

# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method Post -Uri $uri -Body $utf8Bytesn -ContentType "application/json" -UseBasicParsing

$response.RawContent



根据用户 Saj 的建议,我已经测试了从 Fiddler 向 ServiceNow 发布请求。请求已在 ServiceNow 中成功创建。请参阅下面捕获的 Fiddler 会话。

POST https://myinstance.service-now.com/api/sn_sc/v1/servicecatalog/items/3a05b357db2443009906540adc961945/order_now HTTP/1.1
Host: myinstance.service-now.com
User-Agent: Fiddler
Content-Type: application/json
Content-Length: 142
Authorization: Basic myauthorizationcode

{"sysparm_quantity":"1","variables":{"requested_for":"Guest","Request_Type":"New Account","website_login":"2437ffdbdb2443009906540adc96198c"}}

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=ED953CDDB8BB202C5B8D55558003F9B6;Secure; Path=/; HttpOnly
Set-Cookie: glide_user="";Secure; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly
Set-Cookie: glide_user_session="";Secure; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly
Set-Cookie: glide_user_route=glide.c87651ce1f062e6286a35c971e448b2c;Secure; Expires=Thu, 04-Apr-2086 19:33:19 GMT; Path=/; HttpOnly
X-Is-Logged-In: true
Set-Cookie: glide_session_store=B598F91313FC1340B17EDB128144B0E7;Secure; Expires=Sat, 17-Mar-2018 16:49:12 GMT; Path=/; HttpOnly
Pragma: no-store,no-cache
Cache-control: no-cache,no-store,must-revalidate,max-age=-1
Expires: 0
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 17 Mar 2018 16:19:13 GMT
Server: ServiceNow
Set-Cookie: BIGipServerpool_myinstance=2441092106.40510.0000; path=/
Strict-Transport-Security: max-age=63072000; includeSubDomains

59
{"result":{"request_number":"REQ0026993","request_id":"7598fd1313fc1340b17eda128144b043"}
1
}
0
4

1 回答 1

1

感谢@Saj 引导我朝着正确的方向前进,并推荐 Fiddler 帮助检查我对 ServiceNow 的请求和响应。问题是我没有指定应该在我的 json 正文中定义为变量的内容。例如

$body = @{
'sysparm_quantity' = '1'
'variables' = @{
'requested_for' = "$RequestedFor"
'Request_Type' = $RequestType
'website_login' = $Vendor1
}}

我还删除了将 json 转换为 UTF8 数组的代码部分,因为它不是必需的。

$utf8Bytes = [System.Text.Encoding]::UTf8.GetBytes($bodyJson)
于 2018-03-21T18:59:36.620 回答