简体   繁体   English

如何发送 Invoke-restMethod

[英]How do I send an Invoke-restMethod

I am attempting to get an authentication toke from a REST API and I appear to have difficulty in sending - well anything apparently.我正在尝试从 REST API 获取身份验证令牌,但我似乎无法发送 - 显然是什么。 This site requires the Id and password be sent in clear text in the body of the request (curl version):该站点要求在请求正文中以明文形式发送 ID 和密码(curl 版本):

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}"

In PowerShell I have this code:在 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'

When I set the -Method to 'POST' I get the following error:当我将 -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":{}}

If I use the default 'GET' I get this error:如果我使用默认的“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":{}}

I believe the issue is with the u0026 in front of the user name and password then the substitution of @ with the %40.我相信问题在于用户名和密码前面的 u0026 然后用 %40 替换了 @。

I need a nudge if you would.如果你愿意,我需要轻推一下。

So this has been a bear.所以这是一只熊。 The REST API is expecting the ID and password to be in the body of the POST request and in the format of user: , password: so I had to load a string with that information and use it in the Invoke-RESTMethod with -Body REST API 期望 ID 和密码位于 POST 请求的正文中,格式为 user: , password: 所以我必须加载包含该信息的字符串,并在 Invoke-RESTMethod 中使用 -Body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM