1

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.

3
  • 3
    Try it without the Accept header. I can download a zip file from GitHub without it. Also, Invoke-RestMethod has an OutFile parameter if you were unaware. Commented Apr 17, 2021 at 11:26
  • @Ash: Not sure why you mentioned about OutFile parameter. I already have the OutFile parameter in my request. Thanks for the suggestion regarding Accept header. I removed it and I see the file getting downloaded... Commented Apr 18, 2021 at 3:44
  • 1
    I was referring to the fact that you are using the cmdlet Out-File and not the included parameter within Invoke-RestMethod. There is no need to pipe it to the next cmdlet. Glad you got your issue fixed anyway. Commented Apr 19, 2021 at 14:38

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.