如何在调用rest API时更正无效的CSRF令牌错误?

时间:2015-05-01 19:13:53

标签: rest powershell csrf

我有一个脚本,它将查询Web休息服务以查找特定对象的ID。我在Powershell下为单个查询得到的查询工作正常,但我需要运行几百个查询。为每个查询登录真的很慢(而且做法不好)。

我首先让脚本登录到服务器并保存会话。我使用post操作运行其余查询。第一个工作正常。第二个炸弹说:

Invoke-RestMethod : Invalid CSRF Token 
Invalid CSRF Token
An invalid cross-site request forgery token was detected in the request.

代码如下所示:

$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("user", $secpasswd)

$headers = "Content-Type: text/plain","Accept: text/plain" 

#Login it the server to store the session to WebSession.
$login = Invoke-WebRequest -Uri "https://devrhapapp01:8444" -Credential $cred -SessionVariable websesssion

#This one returns correctly.
$Results = Invoke-RestMethod -Uri "https://devrhapapp01:8444/api/components/find" -ContentType "text/plain" -Method Post -Body "Search1" -WebSession $websesssion 
write-host $Results

#This one will give an error.
$Results = Invoke-RestMethod -Uri "https://devrhapapp01:8444/api/components/find" -ContentType "text/plain" -Method Post -Body "Search1" -WebSession $websesssion 
write-host $Results

0 个答案:

没有答案