3

I am using Octokit.net version 0.9.0 (GitHub API for .NET) for getting zip contents of few repositories.

I already have the list of repositories I need but I am having trouble with getting the the content of the repositories as .zip files (called zipball)

What I've tried so far

// ... client = new Client(...);
// some authentication logic...
// some other queries to GitHub that work correctly
var url = "https://api.github.com/repos/SomeUser/SomeRepo/zipball";
var response = await this.client.Connection.Get<byte[]>(
        new Uri(url),
        new Dictionary<string, string>(),
        null);
var data = response.Body;
var responseData = response.HttpResponse.Body;

Problems with my attempts

  1. data is null
  2. responseData.GetType().Name says the responseData is of type string
  3. When I try Encoding.ASCII.GetBytes(response.HttpResponse.Body.ToString()); I get invalid zip file

Value of response.HttpResponse.Body

Quesion

What is the correct way to get zipballs of repositories after being authenticated using Octokit.net library?

I've also opened an issue in octokit.net repository.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • The response data looks like a zip file as "PK" is the identifier for zip. Even if it the binary data is converted to string I would not encode it with ascii (did you try it with `Encoding.utf8.GetBytes()`?) – habakuk Apr 27 '15 at 14:44
  • @habakuk, yes, I did, but still no luck. I've tried all predefined encodings and didn't have any success. – Nikolay Kostov Apr 27 '15 at 15:21

1 Answers1

2

After checking the source of Octokit I think this is not possible (as of version 0.10.0):

See Octokit\Http\HttpClientAdapter.cs

// We added support for downloading images. Let's constrain this appropriately.
if (contentType == null || !contentType.StartsWith("image/"))
{
    responseBody = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}
else
{
    responseBody = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}

The response body (responseData) is converted to an unicode string and, thus, it's mangled and not binary the same. See my PR792 (need to replace the if statement with if (contentType == null || (!contentType.StartsWith("image/") && !contentType.StartsWith("application/")))) to fix this (then it's a byte array. Writing possible using System.IO.File.WriteAllBytes("c:\\test.zip", (byte[])responseData);).

MrTux
  • 32,350
  • 30
  • 109
  • 146