我该如何使用Invoke-WebRequest
来提交所有这些参数?
POST /token HTTP/1.1
Host: login.huddle.net
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&client_id=s6BhdRkqt&redirect_uri=MyAppServer.com/receiveAuthCode&code=i1WsRn1uB
我该如何使用Invoke-WebRequest
来提交所有这些参数?
POST /token HTTP/1.1
Host: login.huddle.net
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&client_id=s6BhdRkqt&redirect_uri=MyAppServer.com/receiveAuthCode&code=i1WsRn1uB
这里是如何将该正文转换为 PowerShell 可以解释的内容。
$body = @{grant_type='authorization_code'
client_id='s6BhdRkqt'
redirect_uri='MyAppServer.com/receiveAuthCode'
code='i1WsRn1uB'}
$contentType = 'application/x-www-form-urlencoded'
Invoke-WebRequest -Method POST -Uri <i>yourUrl</i> -body $body -ContentType $contentType
做类似于这样的事情
$loginPage = Invoke-WebRequest "http:\\website.com\" # invoke login form page
$loginForm = $loginPage.Forms[0] # Get the form to fill
$loginForm.fields["userName"] = "usrnm" # fill the username
$loginForm.fields["password"] = "psswd" # fill the password
$loginPage = Invoke-WebRequest -Uri ("http:\\website.com\") -Method POST -Body $loginForm.fields # Post the form to log-in
[psCustomObject]
。然而,Invoke-WebRequest cmdlet依赖于我们这些作者将我们的-Body
值转换为正确的内容类型。在现代Web中,对于大多数API来说,这几乎总是JSON,但有时不是。与此形成对比的是Invoke-Restmethod
,它假设您正在使用Rest,因此始终将body值转换为JSON。 - FoxDeploy