我正在尝试使用PowerShell中的以下行发出PATCH请求,但它返回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 主体管道传输到 out-host,从而向 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
}