[英]Invoke-RESTMethod PowerShell
I am trying to use the Invoke-Restmethod to call a set of API's, but it fails with the below error, i have also posted the same json format, can some let me know what could be wrong ?我正在尝试使用 Invoke-Restmethod 来调用一组 API,但它失败并出现以下错误,我也发布了相同的 json 格式,有人可以让我知道什么可能是错误的吗?
### Ignore TLS/SSL errors
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Create URL string for Invoke-RestMethod
$urlsend = 'https://' + 'vrslcm-01a.corp.local/lcm/api/v1/' + '/login'
#Credential
$Username = "admin@localhost"
$password = "VMware1!"
$basicAuth = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Username):$Password"))
$headers = @{
"description"= "Testing Authentication"
}
$body = @{
$raw= '{\n\t\"username\": \"admin@localhost\",\n\t\"password\": \"vmware\"\n}'
"mode"= $raw
}
Invoke-RestMethod -Method POST -uri $urlsend -Headers $headers -Body $body -ContentType 'application/json'
Here is the sample jSON which iam trying to invoke via powershell, it consists of the header and the body.这是我试图通过 powershell 调用的示例 jSON,它由标题和正文组成。 I need to understand how we could call the same jSON POSTMAN example via the PowerShell Invoke-RestMethod
我需要了解我们如何通过 PowerShell Invoke-RestMethod 调用相同的 jSON POSTMAN 示例
"item": [
{
"name": "authorization",
"description": "",
"item": [
{
"name": "Login",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"var response=JSON.parse(responseBody)",
"postman.setEnvironmentVariable(\"token\", response.token)"
]
}
}
],
"request": {
"url": "{{Server}}/lcm/api/v1/login",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"username\": \"admin@localhost\",\n\t\"password\": \"vmware\"\n}"
},
"description": ""
},
"response": []
},
{
"name": "Logout",
"request": {
"url": "{{Server}}/lcm/api/v1/logout",
"method": "POST",
"header": [
{
"key": "x-xenon-auth-token",
"value": "{{token}}",
"description": ""
}
],
"body": {},
"description": ""
},
"response": []
}
]
},
make $raw
to a hashtable like将
$raw
变成一个哈希表,例如
$raw = @{
username=$Username
password=$Password
}
add this hashtable to the $body
hashtable将此哈希表添加到
$body
哈希表
$body = @{
mode= $raw
}
but now it still is a hashtable the api cannot use.但现在它仍然是一个 api 无法使用的哈希表。 thus convert it to json like
因此将其转换为json
$jsonBody = $body | ConvertTo-Json
using $jsonBody
should then work when used like使用
$jsonBody
然后应该像使用一样工作
Invoke-RestMethod -Method POST -uri $urlsend -Headers $headers -Body $jsonBody -ContentType 'application/json'
Like the error states, the problem is your hash definition.与错误状态一样,问题在于您的哈希定义。
A null key is not allowed in a hash literal.
哈希文字中不允许使用空键。
PowerShell tries to evaluate $raw as a hash table key. PowerShell 尝试将 $raw 评估为哈希表键。 Since it hasn't been defined before it is null and fails because null is not allowed.
因为它在为空之前没有被定义,并且因为不允许为空而失败。 Try it like this:
像这样尝试:
$raw= '{\n\t\"username\": \"admin@localhost\",\n\t\"password\": \"vmware\"\n}'
$body = @{
"mode"= $raw
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.