Sam Tyson
Sam Tyson

Reputation: 4626

How can I get the binary version of a Github Release Asset using Powershell?

I am following the docs from GitHub Releases, but all I ever get returned is the asset contents.

Here is my powershell script:

$gitHubRepository = "<repo>"
$gitHubUsername = "<username>"
$gitHubApiKey = "<apikey>"

# Locate Latest GitHub Release
$releaseParams = @{
    Uri = "https://api.github.com/repos/$gitHubUsername/$gitHubRepository/releases";
    Method = 'GET';
    Headers = @{
        Authorization = 'Basic ' + [Convert]::ToBase64String(
            [Text.Encoding]::ASCII.GetBytes($gitHubApiKey + ":x-oauth-basic")
        );
    }
    ContentType = 'application/json';
    Body = (ConvertTo-Json $releaseData -Compress)
}

$result = Invoke-RestMethod @releaseParams

$tag = $result.tag_name[0]
Write-Host "Release $tag Found."

$asset = $result.assets[0].url
$ZipFile = $result.assets[0].name
$asset

$releaseAssetParams = @{
    Uri = $asset;
    Method = 'GET';
    Headers = @{
        Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($gitHubApiKey + ":x-oauth-basic"));
    }
    ContentType = 'application/octet-stream';
}

Invoke-RestMethod @releaseAssetParams -OutFile $ZipFile

Executing this script (with the variables set correctly), outputs the asset contents, not the binary (zip) file as expected.

Is the content-type not getting recognized?

Upvotes: 1

Views: 519

Answers (1)

Ivan Zuzak
Ivan Zuzak

Reputation: 18782

You need to specify application/octet-stream in an Accept request header, not a Content-Type request header, because you want to tell the server what you want to accept, not what you're sending.

So, you need to send a header like this:

Accept: application/octet-stream

and not

Content-Type: application/octet-stream

The response from the API will be a redirect to the URL which has the actual asset, so you'll need to make another request (this is expected behavior). If you run into problems, let us know.

Upvotes: 2

Related Questions