同一会话中的Powershell HTTP POST方法

时间:2017-09-15 14:47:07

标签: powershell httprequest

我对html的理解有限,我试图自动填写如下表格:

$JSONLOGIN = @'
{"@type":"login",
"email":"xxx@xxx.com",
"password":"xxxx"
}
'@

$response = Invoke-RestMethod -Uri "http://xxxxxxx/api/users/login" -Method Post -Body $JSONLOGIN -ContentType "application/json"

$response

$JSONADDRATE = @'
{
"@type":"add",
"supplierRef":"S020893",
"buyerRef":"520170509082555",
"fiRef":"7769",
"rateDate":"2017-09-15T14:11:04.684Z",
"rateType":"fixedRate",
"spFee":2,
"comFee":1,
"fiMarginOver":3,
"ttofc":0,"isTtofc":false,
"fixedRate":2,"primeLiborRate":0,
"oneWTenorRate":0,
"oneMTenorRate":0,
"twoMTenorRate":0,
"threeMTenorRate":0,
"sixMTenorRate":0,
"nineMTenorRate":0,
"oneYTenorRate":0}
'@


$response2 = Invoke-RestMethod -Uri "http://xxxxxxx/api/ubiq/rates/add" -Method Post -Body $JSONADDRATE -ContentType "application/json"

$response2

回复就像那样:

$response

isSessionValid : True
data           : @{email=xxx@xxxx.com; id=5988ad267520a62df46af0a4; name=xxx Back Office}
isSuccessful   : True
isError        : False
isAllowed      : True
message   

$response2

isSessionValid : False
data           : 
isSuccessful   : False
isError        : False
isAllowed      : False
message  

显然,当跳转到第二个链接时登录会话丢失(因此isSessionValid:false),我如何保持会话活着?

1 个答案:

答案 0 :(得分:0)

-SessionVariable参数创建Web Request Session Object基本上可以在当前PowerShell会话中保存您的会话。

为了利用这一点,试试这个:

In the first Request add a -SessionVariable parameter and give it a string
(any string without the preceding $) then in second Request pass that string 
(this time with a preceding $) to the -WebSession parameter.