简体   繁体   中英

Getting "(406) Not Acceptable" when trying to download a git repo using powershell

I am trying to download a repo as a zip file using powershell. Below is how I am trying to do it.

$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;

I am getting below error. Any idea how this can be fixed as suggested in the comments..

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: I am able to download the zip file when I directly hit the $repo url in a browser.

EDIT: Updated the Invoke-RestMethod command to use just OutFile instead of piping it.

The HTTP Accept header is an instruction to the server saying

Hey, I can ONLY UNDERSTAND these formats

So, in your request you're saying

Github, I want this file BUT I also want you to do some work for me and make it into this format, kthanxbai

GitHub is responding 406 Unacceptable because it don't have time for you or your weird request formats, and also because you left off the * option at the end, which means "I'll take this in any format".

To fix, try to do the request without an Accepts header, or modify your header like this:

  Accept        = "application/vnd.github.v3.raw,*"

Source:

The Accept header tells the server what file formats, or more correctly MIME-types, the browser is looking for...

https://www.newmediacampaigns.com/blog/browser-rest-http-accept-headers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM