Invoke-RESTMethod PowerShell

问题描述 投票:-1回答:2

我正在尝试使用Invoke-Rest方法来调用一组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'

enter image description here

这是我试图通过powershell调用的示例jSON,它由标题和正文组成。我需要了解如何通过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": []
                }
            ]
        },
powershell
2个回答
1
投票

$raw变成哈希表

$raw = @{
    username=$Username
    password=$Password
}

将此哈希表添加到$body哈希表中

$body = @{         
    mode= $raw
}

但现在它仍然是api无法使用的哈希表。因此将其转换为json就好

$jsonBody = $body | ConvertTo-Json

使用$jsonBody然后应该像使用时一样工作

Invoke-RestMethod -Method POST -uri $urlsend -Headers $headers -Body $jsonBody -ContentType 'application/json'

0
投票

与错误状态一样,问题是您的哈希定义。

散列字面值中不允许使用null键。

PowerShell尝试将$ raw评估为hash table密钥。因为它在null之前尚未定义并且因为不允许null而失败。试试这样:

$raw= '{\n\t\"username\": \"admin@localhost\",\n\t\"password\": \"vmware\"\n}'

$body = @{         
    "mode"= $raw
}
© www.soinside.com 2019 - 2024. All rights reserved.