Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JSON对象通过POST方法调用URL,但使用Powershell_Json_Powershell - Fatal编程技术网

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

使用JSON对象通过POST方法调用URL,但使用Powershell,json,powershell,Json,Powershell,我试图通过POST方法调用特定的URL,其中JSON对象作为POST表单参数传递。需要传递的JSON对象如下所示: obj={ "login":"[username here]", "pword":"[password here]" } 使用Powershell,我试图创建一个散列,然后将其转换为JSON,然后使用Invoke RestMethod命令进行连接 $hash = @{ login = "username"; pword = "password"

我试图通过POST方法调用特定的URL,其中JSON对象作为POST表单参数传递。需要传递的JSON对象如下所示:

obj={
"login":"[username here]",
"pword":"[password here]"
}
使用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'
但是,这将返回一个错误。仔细检查文档,它注意到表单参数名必须是obj,因为web服务专门查找名为obj的参数,获取字符串值,然后将其转换回JSON对象以检索内部值


这就是我被卡住的地方。在使用Powershell时,如何准确指定表单参数名

您提交的表单:

obj={
"login":"[username here]",
"pword":"[password here]"
}
似乎是无效的JSON。所以你必须捏造它:

$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'