谁能帮我使用PowerShell调用,RestMethod
问题描述:
我使用PowerShell调用,RestMethod谁能帮我使用PowerShell调用,RestMethod
- 获取令牌从暴露RESTAPI要设置以下功能。
- 使用收到的令牌检查身份验证。
- 如果身份验证是正确的发送一些数据的主体,包括报头信息。
我有我下面的信息,任何人都可以帮助我,因为我没有太多的RestMethod调用经验。
要获得令牌: http://xx.xx.xx.xx:8080/test/getToken 方法:GET
响应:1fd640d0-b4fc-4b95-A27C-e61b7cb3a44f
到POST数据: http://xx.xx.xx.xx:8080/test/postData 方法:POST
部首帕拉姆:
X-Access-Token: 1fd640d0-b4fc-4b95-a27c-e61b7cb3a44f
样品请求:
{
"id": 2,
"cid": "TEST",
"concurrent_users": 10,
"view_version": "7.0.3",
"timestamp": "2016:12:12:22:00",
"pool_details": [{
"id": 12,
"pool_name": "Test_Pool",
"active_dtps": 10,
"provisioned_dtps": 10
}, {
"id": 12,
"pool_name": "Test_Pool",
"active_dtps": 10,
"provisioned_dtps": 10
}]
}
样品响应:
{
"id": 2,
"cid": "TEST",
"concurrent_users": 10,
"view_version": "5.3.4",
"timestamp": "2016:12:12:22:00",
"pool_details": [{
"id": 12,
"pool_name": "Test_Pool",
"active_dtps": 10,
"provisioned_dtps": 10
}, {
"id": 12,
"pool_name": "Test_Pool",
"active_dtps": 10,
"provisioned_dtps": 10
}]
}
答
未经测试:
$token = Invoke-RestMethod -Uri "http://xx.xx.xx.xx:8080/test/getToken"
Write-Host $token
$data = @{
id=2
cid="TEST"
concurrent_users=10
view_version="7.0.3"
timestamp="2016:12:12:22:00"
[email protected](
{ id=12; pool_name="Test_Pool"; active_dtps=10; provisioned_dtps=10 },
{ id=12; pool_name="Test_Pool"; active_dtps=10; provisioned_dtps=10 }
)
}
$response = Invoke-RestMethod -Uri "http://xx.xx.xx.xx:8080/test/postData" -Method Post -Headers @{'X-Access-Token'=$token} -Body $data
Write-Hot $response
+0
谢谢@craig的回应,我没有测试过这个,它需要很少的修改。 1)主体部分将不得不被转换成一个方法(JSON/HTML),所以我没有使用JSON在我的情况,因为它是在使用中更受欢迎。 2)它需要添加页眉在获取令牌部 3)它还需要在后添加方法 - 身体类型。 –
答
得到了一个答案,它完美的作品。
$token_url = "http://xx.xx.xx.xx:8080/test/getToken"
$username = "varun"
$password = "password"
$post_data_url = "http://xx.xx.xx.xx:8080/test/postData"
$pool = (
@{
id = 12
pool_name = "Test_Pool"
active_dtps = 10
provisioned_dtps = 10
},
@{
id = 2
cid = "TEST"
concurrent_users = 10
view_version = "10.25"
timestamp = "2016:12:12:22:00"
pool_details = $pool
}
)
$request = @{
id = 2
cid = "TEST"
concurrent_users = 10
view_version = "10.25"
timestamp = "2016:12:12:22:00"
pool_details = $pool # | ConvertTo-Json
}
$json = $request | ConvertTo-Json
$token = Invoke-RestMethod -Method Get -Uri $token_url -Headers @{ "Authorization" = "username=$username;password=$password"}
$result = Invoke-RestMethod -Method Post -Uri $post_data_url -Headers @{ "X-Access-Token" = "$token"} -ContentType 'application/json' -Body $json
$result
我是新的Invoke-RestMethod,所以我没有尝试太多,希望有人谁可以帮助我与相同的示例编码。 –