Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
PowerShell 403 HTTP请求,参数包含方括号_Powershell_Http_Https_Httpwebrequest - Fatal编程技术网

PowerShell 403 HTTP请求,参数包含方括号

PowerShell 403 HTTP请求,参数包含方括号,powershell,http,https,httpwebrequest,Powershell,Http,Https,Httpwebrequest,我正在尝试使用PowerShell中的以下行发出修补程序请求,但它返回403: Try{ $Body = @{ 'api_key' = 'myapikey' 'item[status]' = 'unavailable' } | ConvertTo-Json $response = Invoke-WebRequest -Method PATCH -Uri "https://api.example.com/store/apikey.json" -B

我正在尝试使用PowerShell中的以下行发出修补程序请求,但它返回403:

Try{
    $Body = @{
       'api_key' = 'myapikey'
       'item[status]' = 'unavailable'
    } | ConvertTo-Json
    $response = Invoke-WebRequest -Method PATCH -Uri "https://api.example.com/store/apikey.json" -Body $Body -ContentType "application/json"
    $response.StatusCode
}
Catch [System.Net.WebException]{
    $code = [int]$_.Exception.Response.StatusCode
}
Fiddler返回403,并显示以下消息:“{”错误“:“参数项是必需的”}”。此外,Fiddler中的查询字符串为空。但是,当所有内容都硬编码到Uri中时,会发出成功的请求:

$statusUpdate = Invoke-WebRequest -Method PATCH -Uri "https://api.example.com/store/apikey.json?api_key=myapikey&item[status]=unavailable" -ContentType "application/json"

如果您实际使用的是这段代码来访问API,那么您的问题似乎只是由于将json主体输送到主机外,从而向API发送了一个空的$body,因此您应该对其进行编辑:

try
{
  $Body = @{
    'api_key' = 'myapikey'
    'item[status]' = 'unavailable'
  } | ConvertTo-Json
  $response = Invoke-WebRequest -Method PATCH -Uri "https://api.example.com/store/apikey.json" -Body $Body -ContentType "application/json"
  $response.StatusCode
}
catch [System.Net.WebException]
{
  $code = [int]$_.Exception.Response.StatusCode
}

您是否尝试对参数名称进行URL编码?请注意(但与您的问题无关)使用application/json作为修补程序的有效负载类型具有未定义的语义。@AnsgarWiechers您在谈论的是:[System.Web.HttpUtility]::UrlEncode('item[status]')='unavailable',如果是,那么是的,我有。它仍然返回403。JSON请求正文将在Fiddler中显示为item%5bstatus%5d=unavailable。是的,这就是我的意思。不过,这只是瞎猜。很抱歉,我使用了主机进行调试,但它仍然返回403。编辑原始问题以清除任何混淆。