Powershell v3 Invoke-RestMethod 标头 [英] Powershell v3 Invoke-RestMethod Headers

查看:101
本文介绍了Powershell v3 Invoke-RestMethod 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对服务进行 RestAPI 调用,该服务在其文档中指定以下内容:

I am trying to make a RestAPI call to a service which specifies in it's documentation the following:

集成服务器可以以 XML 和 JSON 格式进行响应.在您的文件中使用以下接受标头之一请求:

An Integration Server can respond in XML and JSON formats. Use one of the following accept headers in your requests:

  1. 接受:application/json,/.
  2. 接受:application/xml,/

如果接受标头不包含 application/xml、application/json 或 /,集成服务器将响应406 方法不可接受"状态代码.

If the accept header does not include application/xml, application/json or /, the integration server will respond with a "406 method not acceptable" status code.

我的 powershell 代码如下所示Invoke-RestMethod -URI https://URL/ticket -Credential $cred -Method Get -Headers @{"Accept"="application/xml"}

My powershell code looks like this Invoke-RestMethod -URI https://URL/ticket -Credential $cred -Method Get -Headers @{"Accept"="application/xml"}

但是我收到与标题相关的以下错误:Invoke-RestMethod :必须使用适当的属性或方法修改此标头.参数名称:名称

But I get the following error relating to the header: Invoke-RestMethod : This header must be modified using the appropriate property or method. Parameter name: name

有人可以帮助我理解为什么 powershell 不允许我指定 Accept 标头吗?还是我在这里缺少另一种方法?

Can someone assist me with understanding why powershell wont let me specify the Accept header? Or is there another method I'm missing here?

谢谢

推荐答案

自从 Accept 标头无法为 ="noreferrer">Invoke-RestMethodInvoke-WebRequest 在 PowerShell V3 中,您可以考虑以下在一定程度上模拟 Invoke-RestMethod 的函数:

Since Accept header could not be specified for neither Invoke-RestMethod nor Invoke-WebRequest in PowerShell V3, you could consider the following function that simulates to some extent Invoke-RestMethod:

Function Execute-Request()
{
Param(
  [Parameter(Mandatory=$True)]
  [string]$Url,
  [Parameter(Mandatory=$False)]
  [System.Net.ICredentials]$Credentials,
  [Parameter(Mandatory=$False)]
  [bool]$UseDefaultCredentials = $True,
  [Parameter(Mandatory=$False)]
  [Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get,
  [Parameter(Mandatory=$False)]
  [Hashtable]$Header,  
  [Parameter(Mandatory=$False)]
  [string]$ContentType  
)

   $client = New-Object System.Net.WebClient
   if($Credentials) {
     $client.Credentials = $Credentials
   }
   elseif($UseDefaultCredentials){
     $client.Credentials = [System.Net.CredentialCache]::DefaultCredentials 
   }
   if($ContentType) {
      $client.Headers.Add("Content-Type", $ContentType)
   }
   if($Header) {
       $Header.Keys | % { $client.Headers.Add($_, $Header.Item($_)) }  
   }     
   $data = $client.DownloadString($Url)
   $client.Dispose()
   return $data 
}

示例:

Execute-Request -Url "https://URL/ticket" -UseDefaultCredentials $true

Execute-Request -Url "https://URL/ticket" -Credentials $credentials -Header @{"Accept" = "application/json"} -ContentType "application/json"

这篇关于Powershell v3 Invoke-RestMethod 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆