我如何发送Invoke-restMethod

问题描述 投票:0回答:1

我正在尝试从REST API获取身份验证令牌,并且似乎在发送时遇到困难-显然任何事情。该站点要求在请求正文(curl版本)中以明文形式发送ID和密码:

curl -X POST "https://somehost.myplace.com/api/v2/authorize" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"username\": \"root\", \"password\": \"123@my_place\", \"cookie\": true, \"csrfToken\": false}"

在PowerShell中,我有以下代码:

$headers = @{
    'accept'='application/json';
    'Content-Type'='application/json'
}
$auth_data = @{ 'username'='root';
    'password'='123@my_place';
    'cookie'=$true;
    'csrfToken'=$false
}
$LoginResponse = ""
$LoginResponse = Invoke-WebRequest 'https://somehost.myplace.com/api/v2/authorize' -Headers $headers -Body $auth_data -Method 'POST'

[当我将-Method设置为'POST'时,出现以下错误:

{"message":{"text":"Invalid request.","key":"error.400","developerMessage":"Parsing JSON body failed."},"code":400,"status":"error","responseTime":"2020-05-11T14:28:36.836Z","apiVersion":"2.2","data":{}}

如果使用默认的'GET',则会出现此错误:

Invoke-RestMethod: {"message":{"text":"GET https://somehost.myplace.com/api/v2/authorize?csrfToken=False\u0026cookie=True\u0026username=root\u0026password=123%40my_place does not match a valid endpoint.","key":"error.405","developerMessage":"Route does not match, double-check the URL."},"code":405,"status":"error","responseTime":"2020-05-11T15:01:49.360Z","apiVersion":"2.2","data":{}}

我相信问题在于用户名和密码前面的u0026,然后用%40代替@。

如果需要,我需要轻推。

powershell
1个回答
0
投票

所以这一直是熊。 REST API期望ID和密码位于POST请求的主体中,并采用user:,password:的格式,因此我必须加载带有该信息的字符串,并在带有-Body的Invoke-RESTMethod中使用它] >

© www.soinside.com 2019 - 2024. All rights reserved.