git - 尝试使用 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
而不是管道它。
解决方案
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
推荐阅读
- r - R Rstudio 中的 SDM 包出错:模型无法运行,因为它找不到“存在插槽”
- amazon-web-services - AWS Cognito 跨区域复制
- reactjs - 在 Material-UI Datagrid 表的每个数据行之间添加一个空格/行
- qt - Qt:样式取决于标签值
- python - 如何从对数极坐标转换为视网膜视图?
- c# - WebHost 启动时的事件或回调
- elasticsearch - 如何在 Elasticsearch 中获取每个术语的统计信息
- oracle - 如何在 Windows Server 2016 上更新 OLEDB 驱动程序?
- material-components-android - 在 Jetpack Compose 中使用自定义图像或文本样式实现可重用 Composable
- javascript - Chrome 扩展程序:如何访问 iframe 源代码?