我正在尝试使用 powershell 将存储库下载为 zip 文件。下面是我如何尝试做到这一点。
$token = "MyGitToken";
$repo = "https://github.com/my-private-repo/archive/master.zip";
$targetFile = "./master.zip";
$restRequest = @{
Method = "Get"
Uri = "$repo"
Headers = @{
Authorization = "Token $token"
Accept = "application/vnd.github.v3.raw"
}
};
$response = Invoke-RestMethod @restRequest -OutFile $targetFile;
我得到低于错误。知道如何按照评论中的建议修复此问题。
System.Net.WebException: The remote server returned an error: (406) Not Acceptable.
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
PS:当我在浏览器中直接点击 $repo url 时,我可以下载 zip 文件。
编辑:将Invoke-RestMethod
命令更新为仅使用OutFile
而不是管道它。
HTTP Accept 标头是对服务器说的指令
嘿,我只能理解这些格式
所以,在您的请求中,您说的是
Github,我想要这个文件,但我也想让你帮我做一些工作,把它做成这种格式,kthanxbai
GitHub 响应 406 Unacceptable 因为它没有时间处理您或您奇怪的请求格式,还因为您*
在最后没有选择该选项,这意味着“我将采用任何格式”。
要修复,请尝试在没有 Accepts 标头的情况下执行请求,或者像这样修改标头:
Accept = "application/vnd.github.v3.raw,*"
来源:
Accept 标头告诉服务器浏览器正在寻找什么文件格式,或更准确地说是 MIME 类型...
https://www.newmediacampaigns.com/blog/browser-rest-http-accept-headers
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句