这是我在Stackoverflow上的第一篇文章,如果我的帖子没有正确格式化或提交,请告诉我。我试图使用powershell连接到我的ServiceNow实例并POST一个服务目录请求。但是,我收到错误(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/3a05b357db2443009906540adc961945/order_now
与原始响应主体:
{" sysparm_quantity":" 1""变量" {" requested_for":"家庭"&#34 ; Request_Type":"新帐户"," website_login":" 2437ffdbdb2443009906540adc96198c"}}
我也能够使用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
答案 0 :(得分:0)
感谢@Saj带领我朝着正确的方向前进,并建议Fiddler帮助检查我的请求和对ServiceNow的响应。问题是我没有在json体中指定应该定义为变量的内容。 E.g。
$body = @{
'sysparm_quantity' = '1'
'variables' = @{
'requested_for' = "$RequestedFor"
'Request_Type' = $RequestType
'website_login' = $Vendor1
}}
我还删除了将json转换为UTF8数组的代码部分,因为它没有必要。
$utf8Bytes = [System.Text.Encoding]::UTf8.GetBytes($bodyJson)