首页 > 解决方案 > 尝试使用 powershell 下载 git repo 时出现“(406)不可接受”

问题描述

我正在尝试使用 powershell 将 repo 下载为 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而不是管道它。

标签: gitpowershell

解决方案


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


推荐阅读