调用RESTMethod PowerShell

时间:2018-11-07 06:11:10

标签: powershell

我正在尝试使用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'

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": []
                }
            ]
        },

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分:0)

就像错误状态一样,问题出在您的哈希定义上。

  

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

PowerShell尝试将$ raw评估为hash table键。由于尚未定义它,所以它为null并因不允许null而失败。像这样尝试:

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

$body = @{         
    "mode"= $raw
}