我正在尝试从REST API获取身份验证toke,但我似乎在发送时遇到了困难--显然是任何事情。本网站要求在请求正文(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替换@。
如果你愿意的话,我需要你的帮助。
发布于 2020-05-12 05:05:25
因此,这是一只熊。REST API期望ID和密码位于POST请求的主体中,格式为user:,password:,因此我必须加载一个包含该信息的字符串,并在带有-Body的Invoke-RESTMethod中使用它
https://stackoverflow.com/questions/61734301
复制相似问题