我想使用Powershell Invoke-RestMethod
设置以下功能我有以下信息,有人可以帮助我,因为我没有太多的RestMethod呼叫经验。
获取令牌: http://xx.xx.xx.xx:8080/test/getToken 方法:GET
回应:1fd640d0-b4fc-4b95-a27c-e61b7cb3a44f
发布数据: http://xx.xx.xx.xx:8080/test/postData 方法:POST
Header Param:
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
}]
}
答案 0 :(得分:1)
得到答案,效果很好。
$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
答案 1 :(得分:0)
未测试:
$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"
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 }
)
}
$response = Invoke-RestMethod -Uri "http://xx.xx.xx.xx:8080/test/postData" -Method Post -Headers @{'X-Access-Token'=$token} -Body $data
Write-Hot $response