简体   繁体   English

使用JSON对象通过POST方法调用URL,但使用Powershell

[英]Calling a URL via POST method with JSON object, but using Powershell

I am attempting to call a specific URL via POST method, where a JSON object is passed as a POST form parameter. 我试图通过POST方法调用特定的URL,其中JSON对象作为POST表单参数传递。 The JSON object that needs to be passed is something like this: 需要传递的JSON对象是这样的:

obj={
"login":"[username here]",
"pword":"[password here]"
}

With Powershell, I was attempting to create a hash and then convert that into JSON, then connect with the Invoke-RestMethod command. 使用Powershell,我试图创建一个哈希,然后将其转换为JSON,然后使用Invoke-RestMethod命令连接。

$hash = @{  login = "username"; 
            pword = "password"
            }

$obj = $hash | convertto-json
Invoke-RestMethod 'https://website.com/login' -Method POST -Body $obj -ContentType 'application/x-www-form-urlencoded'

However, this returns an error. 但是,这会返回错误。 Double-checking the documentation, it notes that the form parameter name MUST be obj , as the web services looks specifically for a parameter called obj , takes the string value, then converts it back to a JSON object to retrieve the internal values. 仔细检查文档,它注意到表单参数名称必须是obj ,因为Web服务专门查找名为obj的参数,获取字符串值,然后将其转换回JSON对象以检索内部值。

This is where I am getting a little stuck. 这是我陷入困境的地方。 How exactly can I specific a form parameter name when using Powershell? 使用Powershell时,我如何具体确定表单参数名称?

The form you've presented: 您提供的表格:

obj={
"login":"[username here]",
"pword":"[password here]"
}

Appears to be invalid JSON. 似乎是无效的JSON。 So.. you'd have to fudge it: 那么..你必须捏造它:

$hash = @{  login = "username"; 
            pword = "password"
            }

$obj = $hash | convertto-json
$obj = 'obj=' + $obj
Invoke-RestMethod 'https://website.com/login' -Method POST -Body $obj -ContentType 'application/x-www-form-urlencoded'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM